demo.js:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection; // 获取数据库的连接对象。
db.once('open', function (callback) {
console.log("数据库成功打开");
});
// Schema 博客的结构 (表结构)
var blogSchema = new mongoose.Schema({
title: String,
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
// 注册实例方法(methods) (静态方法用statics)
blogSchema.methods.showInfo = function(){
console.log(this.title); // this指向该实例(对象)
}
var Blog = mongoose.model('Blog', blogSchema); // 根据schema创建模型(类)
// 第一种方式:创建实例
var blog = new Blog({
"title" : "博客测试",
"author"