MongoDB,从数组对象中删除嵌套项

时间:2020-09-01 12:24:55

标签: mongodb unset

这是我的数据结构:

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

我要删除的是那些字段:minute中所有条目的所有coach中的goalsmatchescompetitionmatches

我在Robo 3T中尝试了以下成功的方法:

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

如果我通过otherField,则会很好地删除matches的每个元素中的整个数组multi:true(为便于阅读,在此删除)。

但是当我尝试:

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

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

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

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

取消设置有效,但路径似乎是我更新的错误部分。

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

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

1 个答案:

答案 0 :(得分:0)

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

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
    } 
  } 
);

Mongo shell

> 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: [ {}, {} ]
      }
    ]
  }
]
>