首页 > 解决方案 > MongoDB,从数组中的对象中删除嵌套项

问题描述

这是我的数据结构:

db.getCollection('competitionmatches').find()
{
    "_id" : ObjectId("5d2d9eed5972a9367cd5010a"),
    "matches" : [ 
        {
            "id" : 200000,
            "utcDate" : "",
            "minute" : null,
            "homeTeam" : {
                "id" : 808,
                "coach" : {},
            },
            "goals" : [{},{}}],
        },
        {...},
        {...},
    ],
    "otherField": []
},
{...},
{...},

我要删除的是那些字段:minutecoach以及.goalsmatchescompetitionmatches

我在 Robo 3T 中成功尝试了以下内容:

db.getCollection('competitionmatches').update({}, {$unset: {"otherField":""}})

这很好地删除了if i passotherField的每个元素中的整个数组(为了便于阅读,在此处删除)。matchesmulti:true

但是当我尝试时:

db.getCollection('competitionmatches').update({}, {$unset: {"matches.$[].goals":""}})

或者

db.getCollection('competitionmatches').update({}, {$unset: {"matches.$[].minute":""}})

我收到一条成功消息Updated 1 existing record(s) in 3ms,但记录保持不变,minute或者goals不被删除。

我从Remove a field from all elements in mongodb 中得到了这个答案,并将我的 mongo 版本从 3.4 更新到 3.6,因为$[]正如svr所说,它是在 3.6 中引入的,并在文档中显示。

Unset 有效,但路径似乎是我更新的错误部分。

"matches.$.goals"返回错误并且matches.[].goals也没有任何影响。我什至想知道是否可能有一些缓存,但这没有多大意义,因为 unset onotherField执行得很好。

我也怀疑更新是否顺利,但据我所知,使用数据库的应用程序运行良好。

标签: mongodbunset

解决方案


更新无法使用$or访问直接数组字段$[],我们需要提供查询以匹配数组,

db.getCollection('competitionmatches').update(
  // it requires to specify any one field match condition in query part
  { "matches.goals": { $exists: true } }, 
  { 
    $unset: { 
      "matches.$[].minute": true // pass true
      "matches.$[].homeTeam.coach": true // pass true
      "matches.$[].goals": true // pass true
    } 
  } 
);

蒙哥壳

> db.upd22.find()
[
  {
    _id: ObjectId("5d2d9eed5972a9367cd5010a"),
    matches: [
      {
        id: 200000,
        utcDate: '',
        minute: null,
        homeTeam: { id: 808, coach: {} },
        goals: [ {}, {} ]
      },
      {
        id: 300000,
        utcDate: '',
        minute: null,
        homeTeam: { id: 808, coach: {} },
        goals: [ {}, {} ]
      }
    ]
  },
  {
    _id: ObjectId("5d2d9eed5972a9367cd5010b"),
    matches: [
      {
        id: 400000,
        utcDate: '',
        minute: null,
        homeTeam: { id: 808, coach: {} },
        goals: [ {}, {} ]
      },
      {
        id: 500000,
        utcDate: '',
>                                                                                                                                          homeTeam: { id: 808, coach: {} },
        goals: [ {}, {} ]
      }
    ]
  }
]
> db.getCollection('upd22').update(
...     { "matches.goals": { $exists: true } },
...     {
.....         $unset: {
.......             "matches.$[].minute": true,
.......             "matches.$[].goals": true
.......         }
.....     },
...     { multi: false }
... )
{
  acknowledged: 1,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
> db.upd22.find()
[
  {
    _id: ObjectId("5d2d9eed5972a9367cd5010a"),
    matches: [
      { id: 200000, utcDate: '', homeTeam: { id: 808, coach: {} } },
      { id: 300000, utcDate: '', homeTeam: { id: 808, coach: {} } }
    ]
  },
  {
    _id: ObjectId("5d2d9eed5972a9367cd5010b"),
    matches: [
      {
        id: 400000,
        utcDate: '',
        minute: null,
        homeTeam: { id: 808, coach: {} },
        goals: [ {}, {} ]
      },
      {
        id: 500000,
        utcDate: '',
        minute: null,
        homeTeam: { id: 808, coach: {} },
        goals: [ {}, {} ]
      }
    ]
  }
]
>


推荐阅读