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
小伙看你根骨奇佳,潛力無限,來學(xué)PHP伐。
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);
});
Set the upsert attribute of the third parameter of the update method to true
Book.update(
// 查詢
{
name: "The Kite Runner"
},
// 更新
{
auther: "Khaled Hosseini"
},
// 其他參數(shù)
{
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;