我有以下收藏
{
    "_id" : ObjectId("57315ba4846dd82425ca2408"),
    "myarray" : [ 
        {
            userId : ObjectId("570ca5e48dbe673802c2d035"),
            point : 5
        },
        {
            userId : ObjectId("613ca5e48dbe673802c2d521"),
            point : 2
        },        
     ]
}这些是我的问题
如果myarray不存在,我想插入userId,它应该附加到myarray中。如果存在userId,则应将其更新为指向。
我发现了这个
db.collection.update({
    _id : ObjectId("57315ba4846dd82425ca2408"),
    "myarray.userId" :  ObjectId("570ca5e48dbe673802c2d035")
}, {
    $set: { "myarray.$.point": 10 }
})但是如果userId不存在,什么都不会发生。
和
db.collection.update({
    _id : ObjectId("57315ba4846dd82425ca2408")
}, {
  $push: {
      "myarray": {
          userId: ObjectId("570ca5e48dbe673802c2d035"),
          point: 10
      }
  }
})但是,如果userId对象已经存在,它将再次推送。
在MongoDB中做这件事的最好方法是什么?
发布于 2016-05-25 08:06:34
尝尝这个
db.collection.update(
    { _id : ObjectId("57315ba4846dd82425ca2408")},
    { $pull: {"myarray.userId": ObjectId("570ca5e48dbe673802c2d035")}}
)
db.collection.update(
    { _id : ObjectId("57315ba4846dd82425ca2408")},
    { $push: {"myarray": {
        userId:ObjectId("570ca5e48dbe673802c2d035"),
        point: 10
    }}
)解释:在第一个状态中,$pull从文档( _id =  ObjectId("57315ba4846dd82425ca2408") )上的数组中移除带有userId= ObjectId("570ca5e48dbe673802c2d035")的元素
在第二个数组中,$push将此对象{ userId:ObjectId("570ca5e48dbe673802c2d035"), point: 10 }插入到同一个数组中。
发布于 2018-02-28 18:58:06
费舍尔接受的答案是,现有的记录将首先被删除,然后再被推送。
一种更安全的方法(常识)是尝试首先更新记录,如果没有找到匹配的记录,就插入它,如下所示:
// 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
                }
            }
        }
    );
}这也会提高性能(常识,未经测试),如果在大多数情况下记录已经存在,则只执行第一个查询。
发布于 2021-01-30 11:48:07
有一个名为使用聚合管道更新文档的选项,从MongoDB v4.2开始,
$cond是否userId在myarray.userId中$map迭代myarray数组的循环并检查userId是否匹配,然后使用$mergeObjects与新文档合并。$concatArrays连接新对象和myarraylet _id = ObjectId("57315ba4846dd82425ca2408");
let uodateDoc = {
  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] },
                      uodateDoc,
                      {}
                    ]
                  }
                ]
              }
            }
          },
          { $concatArrays: ["$myarray", [uodateDoc]] }
        ]
      }
    }
  }]
)https://stackoverflow.com/questions/37427610
复制相似问题