【问题标题】:How to remove element from all sub arrays in MongoDB如何从MongoDB中的所有子数组中删除元素
【发布时间】:2020-06-22 23:00:02
【问题描述】:

如何删除“newFor”中的所有“aFHJBrKu54y5mWjY3”?

尝试这样做,但它不起作用。

        Messages.update({
                users: { $all: ["CnugxoBWs4ox6vG2k", "aFHJBrKu54y5mWjY3"] }
            },
            {
                $pull: {
                    "messages.$.newFor": "aFHJBrKu54y5mWjY3"
                }
            },
            { multi: true }
        );
{
    "_id" : "gQnYi2e54zADwgipx",
    "users" : [ 
        "aFHJBrKu54y5mWjY3", 
        "CnugxoBWs4ox6vG2k"
    ],
    "lastMessageAt" : ISODate("2020-06-22T22:50:35.579Z"),
    "messages" : [ 
        {
            "id" : "4d2219d05645a3991d3aae89",
            "addedAt" : ISODate("2020-06-22T22:50:35.579Z"),
            "userId" : "CnugxoBWs4ox6vG2k",
            "newFor" : [ 
                "aFHJBrKu54y5mWjY3",
                "CnugxoBWs4ox6vG2k"
            ],
            "message" : "Test"
        }, 
        {
            "id" : "b42641118bb080cb9122062f",
            "addedAt" : ISODate("2020-06-22T22:48:24.359Z"),
            "userId" : "aFHJBrKu54y5mWjY3",
            "newFor" : [
                "aFHJBrKu54y5mWjY3"
            ],
            "message" : "Test 2"
        }, 
        {
            "id" : "244e77bb8324dc0b0f0e2def",
            "addedAt" : ISODate("2020-06-22T22:48:14.643Z"),
            "userId" : "CnugxoBWs4ox6vG2k",
            "newFor" : [ 
                "aFHJBrKu54y5mWjY3"
            ],
            "message" : "Test 3"
        }
    ]
}

【问题讨论】:

    标签: arrays mongodb mongodb-query


    【解决方案1】:

    测试一下我的答案:https://mongoplayground.net/p/vHx3J4zQgWr

    查询:

    db.collection.aggregate([
      {
        "$project": {
          "messages": {
            // for every element of messages array
            "$map": {
              "input": "$messages",
              "as": "m",
              "in": {
                // keep id
                "id": "$$m.id",
                // keep addedAt
                "addedAt": "$$m.addedAt",
                // keep userId
                "userId": "$$m.userId",
                // keep message
                "message": "$$m.message",
                // for every element of newFor, remove based on filter
                // $ne means "not equal"
                "newFor": {
                  "$filter": {
                    "input": "$$m.newFor",
                    "as": "n",
                    "cond": {
                      "$ne": [
                        "$$n",
                        "aFHJBrKu54y5mWjY3"
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    ])
    

    结果:

    [
      {
        "_id": "gQnYi2e54zADwgipx",
        "messages": [
          {
            "addedAt": ISODate("2020-06-22T22:50:35.579Z"),
            "id": "4d2219d05645a3991d3aae89",
            "message": "Test",
            "newFor": [
              "CnugxoBWs4ox6vG2k"
            ],
            "userId": "CnugxoBWs4ox6vG2k"
          },
          {
            "addedAt": ISODate("2020-06-22T22:48:24.359Z"),
            "id": "b42641118bb080cb9122062f",
            "message": "Test 2",
            "newFor": [],
            "userId": "aFHJBrKu54y5mWjY3"
          },
          {
            "addedAt": ISODate("2020-06-22T22:48:14.643Z"),
            "id": "244e77bb8324dc0b0f0e2def",
            "message": "Test 3",
            "newFor": [],
            "userId": "CnugxoBWs4ox6vG2k"
          }
        ]
      }
    ]
    

    【讨论】:

      【解决方案2】:

      positional operator$ 只匹配满足查询的第一个数组元素。由于您没有在查询中为messages 指定任何条件,因此它不会匹配任何内容。

      为了从嵌套在messages 数组中的匿名对象中的每个newFor 数组中删除元素,您需要迭代messages 数组,并分别过滤每个newFor 数组。

      如果您使用的是 MongoDB 4.2+,则只能在单个更新命令中使用更新数据库命令的管道形式。

      db.messages.update({},[
          {$set:{
              messages:{
                  $map:{
                      input:"$messages",
                      in:{
                          $mergeObjects:[
                              "$$this",
                              {newFor:{
                                  $filter:{
                                      input:"$$this.newFor",
                                      as:"new",
                                      cond:{$not:{$in:[
                                          "$$new",        
                                          ["aFHJBrKu54y5mWjY3","CnugxoBWs4ox6vG2k"]
                                      ]}}
                                  }
                               }}
                           ]
                      }
                   }
              }
          }}
      ])
      

      【讨论】:

        猜你喜欢
        • 2020-04-16
        • 1970-01-01
        • 1970-01-01
        • 2017-12-22
        • 2017-03-25
        • 2020-08-19
        • 2015-06-12
        • 2013-01-06
        相关资源
        最近更新 更多