简介
var mongoose = require(‘mongoose‘) mongoose.connect(‘mongodb://123.206.112.122:27017/blog‘) var db = mongoose.connection; db.on(‘error‘, console.error.bind(console, ‘connection error:‘)); db.once(‘open‘, function() { console.log(‘hehe‘) var essaySchema = mongoose.Schema({ id:Number, title:String, content:String }) essaySchema.methods.log = function(){ console.log(this.title) } var Essay = mongoose.model(‘Essay‘,essaySchema) var essay1 = new Essay({id:1,title:‘essay title‘,content:‘1‘}) essay1.save(function(err,essay1){ if(err) return console.error(err); essay1.log() }) });
时间: 2024-10-28 16:10:41