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
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
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;