Know that mongodb can insert when there is no data and update when there is data by setting the third parameter of update to true.
How to pass in this parameter to the update method encapsulated by mongoose?
The three parameters of update(doc, options, callback) are provided in the mongoose documentation
仅有的幸福2017-05-02 09:26:06
http://mongoosejs.com/docs/ap...
MyModel.update({ name: 'Tobi' }, { ferret: true }, { upsert: true }, function (err, raw) {
if (err) return handleError(err);
console.log('The raw response from Mongo was ', raw);
});
巴扎黑2017-05-02 09:26:06
Set the upsert attribute of the third parameter of the update method to true
Book.update(
// 查询
{
name: "The Kite Runner"
},
// 更新
{
auther: "Khaled Hosseini"
},
// 其他参数
{
upsert: true,
}, function(err, doc)
{
if (err) console.log(err);
console.log(doc);
});
Update the document’s auther attribute when The Kite Runner exists in the database;
When there is no The Kite Runner in the database, insert The Kite Runner document;