MongoDB - 在数组中更新或插入对象

Nar*_*ham 21 mongodb

我有以下收藏

{
    "_id" : ObjectId("57315ba4846dd82425ca2408"),
    "myarray" : [ 
             {
                userId : ObjectId("570ca5e48dbe673802c2d035"),
                point : 5
             },
             {
                userId : ObjectId("613ca5e48dbe673802c2d521"),
                point : 2
             },        
     ]
}
Run Code Online (Sandbox Code Playgroud)

这些是我的问题

如果userId不存在,我想进入myarray,它应该被附加到myarray.如果userId存在,则应将其更新为point.

我找到了这个

db.collection.update({
  _id : ObjectId("57315ba4846dd82425ca2408"), "myarray.userId" :  ObjectId("570ca5e48dbe673802c2d035")
},{
   $set: {"myarray.$.point": 10}
})
Run Code Online (Sandbox Code Playgroud)

但是如果userId不存在,则没有任何反应.

db.collection.update({
  _id : ObjectId("57315ba4846dd82425ca2408")
},{
  $push: {"myarray": {userId: ObjectId("570ca5e48dbe673802c2d035"), point: 10}}
})
Run Code Online (Sandbox Code Playgroud)

但是如果userId对象已经存在,它将再次推送.

在MongoDB中执行此操作的最佳方法是什么?

tur*_*hal 20

从MongoDB v4.2开始,有一个名为“使用聚合管道更新文档”的选项,

  • 检查条件$cond是否userId存在myarray.userId
  • 如果是,则$map迭代myarray数组循环并检查条件是否userId匹配,然后使用与新文档合并$mergeObjects
  • 如果没有则$concatArrays连接新对象并myarray
let _id = ObjectId("57315ba4846dd82425ca2408");
let updateDoc = {
  userId: ObjectId("570ca5e48dbe673802c2d035"),
  point: 10
};

db.collection.update(
  { _id: _id },
  [{
    $set: {
      myarray: {
        $cond: [
          { $in: [updateDoc.userId, "$myarray.userId"] },
          {
            $map: {
              input: "$myarray",
              in: {
                $mergeObjects: [
                  "$$this",
                  {
                    $cond: [
                      { $eq: ["$$this.userId", updateDoc.userId] },
                      updateDoc,
                      {}
                    ]
                  }
                ]
              }
            }
          },
          { $concatArrays: ["$myarray", [updateDoc]] }
        ]
      }
    }
  }]
)
Run Code Online (Sandbox Code Playgroud)

操场


Yet*_*eti 17

Flying Fisher接受的答案是,现有记录将首先被删除,然后再次被推送.

一种更安全的方法(常识)是首先尝试更新记录,如果找不到匹配,请插入它,如下所示:

// first try to overwrite existing value
var result = db.collection.update(
   {
       _id : ObjectId("57315ba4846dd82425ca2408"),
       "myarray.userId": ObjectId("570ca5e48dbe673802c2d035")
   },
   {
       $set: {"myarray.$.point": {point: 10}}
   }
);
// you probably need to modify the following if-statement to some async callback
// checking depending on your server-side code and mongodb-driver
if(!result.nMatched)
{
    // record not found, so create a new entry
    // this can be done using $addToSet:
    db.collection.update(
        {
            _id: ObjectId("57315ba4846dd82425ca2408")
        },
        {
            $addToSet: {
                myarray: {
                    userId: ObjectId("570ca5e48dbe673802c2d035"),
                    point: 10
                }
            }
        }
    );
    // OR (the equivalent) using $push:
    db.collection.update(
        {
            _id: ObjectId("57315ba4846dd82425ca2408"),
            "myarray.userId": {$ne: ObjectId("570ca5e48dbe673802c2d035"}}
        },
        {
            $push: {
                myarray: {
                    userId: ObjectId("570ca5e48dbe673802c2d035"),
                    point: 10
                }
            }
        }
    );
}
Run Code Online (Sandbox Code Playgroud)

这也应该给(常识,未经测试)性能提高,如果在大多数情况下记录已经存在,则只执行第一个查询.


Fly*_*her 10

试试这个

db.collection.update(
    { _id : ObjectId("57315ba4846dd82425ca2408")},
    { $pull: {"myarray.userId": ObjectId("570ca5e48dbe673802c2d035")}}
)
db.collection.update(
    { _id : ObjectId("57315ba4846dd82425ca2408")},
    { $push: {"myarray": {
        userId:ObjectId("570ca5e48dbe673802c2d035"),
        point: 10
    }}
)
Run Code Online (Sandbox Code Playgroud)

  • 明确您的代码对未来读者的作用将非常有用 (3认同)