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});
1// Using queries with promise chaining
2Model.findOne({ name: 'Mr. Anderson' }).
3 then(doc => Model.updateOne({ _id: doc._id }, { name: 'Neo' })).
4 then(() => Model.findOne({ name: 'Neo' })).
5 then(doc => console.log(doc.name)); // 'Neo'
6
7// Using queries with async/await
8const doc = await Model.findOne({ name: 'Neo' });
9console.log(doc.name); // 'Neo'
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})
1router.patch('/:id', (req, res, next) => {
2 const id = req.params.id;
3 Product.findByIdAndUpdate(id, req.body, {
4 new: true
5 },
6 function(err, model) {
7 if (!err) {
8 res.status(201).json({
9 data: model
10 });
11 } else {
12 res.status(500).json({
13 message: "not found any relative data"
14 })
15 }
16 });
17});
18