如何在mongoDB中删除整个嵌套数组

时间:2019-06-13 10:44:46

标签: mongodb

我有一个名为myCollection的mongoDB集合,在myCollection内部有不同的文档,其结构如下:

{
    "id": 1234,
    "posts": [
       [
          {
             "id": "0.0",
             "name": "john",
             "message": "hello"
          },
          {
             "id": "0.1",
             "name": "jane",
             "message": "good morning"
          },
          {
             "id": "0.2",
             "name": "josh",
             "message": "good evening"
          }
       ],
       [
          {
             "id": "1.0",
             "name": "mark",
             "message": "good lunch"
          }
       ],
       [
          {
             "id": "2.0",
             "name": "john",
             "message": "bye bye"
          },
          {
             "id": "2.1",
             "name": "mark",
             "message": "hi"
          }
       ]
    ]
}

谁能告诉我如何删除包含特定对象的整个数组(嵌套在主要帖子数组中)?例如,围绕id = 1234的文档的此特定对象启动查询:

{
    "id": "2.0",
    "name": "john",
    "message": "bye bye"
}

我希望id = 1234的对象的原始结构变成这样:

{
        "id": 1234,
        "posts": [
           [
              {
                 "id": "0.0",
                 "name": "john",
                 "message": "hello"
              },
              {
                 "id": "0.1",
                 "name": "jane",
                 "message": "good morning"
              },
              {
                 "id": "0.2",
                 "name": "josh",
                 "message": "good evening"
              }
           ],
           [
              {
                 "id": "1.0",
                 "name": "mark",
                 "message": "good lunch"
              }
           ]
        ]
}

0 个答案:

没有答案