我在这里查看 Mongoose 中 Document#update 的示例:
http://mongoosejs.com/docs/api.html#document_Document-update
方法签名:
Document#update(doc, options, callback)
示例代码:
weirdCar.update({$inc: {wheels:1}}, { w: 1 }, callback);
文档说第一个参数应该是“doc”,但doc到底是什么?我想 doc 应该只是一个将键映射到要更新的新值的对象(默认情况下使用 $set)。在代码示例中,他们试图将轮子增加 1。
然后在示例中,他们将 {w : 1} 作为选项传递,但根据 Model.Update,“w”不是有效选项。唯一有效的选项应该是:safe、upsert、multi、strict。
http://mongoosejs.com/docs/api.html#model_Model.update
有人可以解释一下 Mongoose 提供的示例代码吗?
最佳答案
对我来说关键是正确的参数。您需要提供 callback
参数或对结果调用 .exec()
才能使其正常工作。
var Product = mongoose.model('product', mongoose.Schema({
name: String
}));
Product.findById('539dceccc61fa4950b43423a', function (err, product) {
product.update({ name: 'test' }, null, function(err, numberAffected, raw) { });
//or
product.update({ name: 'test' }).exec();
});
关于mongodb - 如何在 Mongoose 中使用 Document#update?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18349650/