var mongoose = require('mongoose'); var db = mongoose.createConnection('mongodb://localhost/test'); mongoose.Promise = global.Promise;//不加这一句会提示 //(node:3341) DeprecationWarning: Mongoose: //mpromise (mongoose's default promise library) is deprecated, //plug in your own promise library instead: //http://mongoosejs.com/docs/promises.html var blogSchema = new mongoose.Schema({ title: String, author: String, body: String, comments: [{ body: String, date: Date }], }); blogSchema.methods.pinglun = function (obj,callback) { this.comments.push(obj); this.save(); }; var Blog = db.model('Blog',blogSchema); var blog = new Blog({ title:'new blog', author:'nidaye', body:'this is a test' }); blog.pinglun({body:'haha',date:new Date()},function () { console.log('success'); });
转载于:https://my.oschina.net/u/3686663/blog/1573028