1var query = {'username': req.user.username};
2req.newData.username = req.user.username;
3
4MyModel.findOneAndUpdate(query, req.newData, {upsert: true}, function(err, doc) {
5 if (err) return res.send(500, {error: err});
6 return res.send('Succesfully saved.');
7});
8
1app.put('/student/:id', (req, res) => {
2 Student.findByIdAndUpdate(req.params.id, req.body, (err, user) => {
3 if (err) {
4 return res
5 .status(500)
6 .send({error: "unsuccessful"})
7 };
8 res.send({success: "success"});
9 });
10
11});
1var conditions = { name: 'bourne' }
2 , update = { $inc: { visits: 1 }}
3
4Model.update(conditions, update, { multi: true }).then(updatedRows=>{
5
6}).catch(err=>{
7 console.log(err)
8
9})
1const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
2res.n; // Number of documents matched
3res.nModified; // Number of documents modified
1const MyModel = mongoose.model('Test', new Schema({ name: String }));
2const doc = new MyModel();
3
4doc instanceof MyModel; // true
5doc instanceof mongoose.Model; // true
6doc instanceof mongoose.Document; // true
1CommentaireArticleModel.update({ pseudo : 'Atinux'}, { pseudo : 'Nikita' }, { multi : true }, function (err) {
2 if (err) { throw err; }
3 console.log('Pseudos modifiés !');
4});