本小白前几天在项目中用到mongo的findOneAndUpdate,使用过程中碰到一些坑,先总结如下:
用法:
db.collection.findOneAndUpdate(
<filter>,
<update document or aggregation pipeline>, // Changed in MongoDB 4.2
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
upsert: <boolean>,
returnNewDocument: <boolean>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
)
注:1. filter是一个document类型,类似一个find查找的条件。
2. update是一个document类型或者array类型。
例子:
{
_id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
{
_id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
{
_id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
{
_id: 6319, name : "R. Stiles", "assignment" : 2, "points" :