Problem updating mongodb using mongoose
伊谢尔伦
伊谢尔伦 2017-05-02 09:24:06
0
2
615

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
仅有的幸福

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"
},
// 其他参数
{
    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;

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template