Update And Return Document In Mongodb

后端 未结 4 1921
南笙
南笙 2020-11-29 09:39

I want to get updated documents. This is my original code and it succesfully updates but doesnt return the document.

collection.update({ \"code\": req.bo         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 10:28

    Checkout the WriteResult object:

    http://docs.mongodb.org/manual/reference/method/db.collection.update/#writeresults-update

    WriteResult result = collection.update({ "code": req.body.code },{$set:  req.body.updatedFields}, function(err, results) {
                        res.send({error: err, affected: results});
                        db.close();
                    });
    

    result should have something like:

    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    

    If you want the updated results, do another query with the primary key.

提交回复
热议问题