var mongoose=require('mongoose');
var db=mongoose.connect('mongodb://localhost/words');
var wordSchema=require('./word_schema.js').wordSchema;
var Words=mongoose.model('Words',wordSchema);
mongoose.connection.once('open',function(){
var query=Words.findOne().where('word','book');
query.exec(function(err,doc){
console.log('is document new? '+doc.isNew);
console.log('\nbefore save:');
console.log(doc.toJSON());
doc.set('word','Book');
doc.set('first','B');
console.log('\nmodified fields:');
console.log(doc.modifiedPaths());
doc.save(function(err,res){
console.log(res);
mongoose.disconnect();
// Words.findOne({word:'Book'},function(err,res){
// console.log('\nafter save:');
// console.log(res);
// mongoose.disconnect();
// });
});
});
});
数据库: https://github.com/CHINQ/angularnodemongodb/blob/master/mongodb/data/generate_data.js word_schema.js : https://github.com/CHINQ/angularnodemongodb/blob/master/mongodb/word_schema.js
1
Mirachael 2016-04-18 11:27:03 +08:00
Model.update(query, update, {new: true}, function(err, result) {})
new: true 表示返回修改后的值 |
2
forl 2016-04-19 10:36:40 +08:00 1
|