mongodb - MongoDB,从数组中的对象中删除嵌套项
问题描述
这是我的数据结构:
db.getCollection('competitionmatches').find()
{
"_id" : ObjectId("5d2d9eed5972a9367cd5010a"),
"matches" : [
{
"id" : 200000,
"utcDate" : "",
"minute" : null,
"homeTeam" : {
"id" : 808,
"coach" : {},
},
"goals" : [{},{}}],
},
{...},
{...},
],
"otherField": []
},
{...},
{...},
我要删除的是那些字段:minute
,coach
以及.goals
matches
competitionmatches
我在 Robo 3T 中成功尝试了以下内容:
db.getCollection('competitionmatches').update({}, {$unset: {"otherField":""}})
这很好地删除了if i passotherField
的每个元素中的整个数组(为了便于阅读,在此处删除)。matches
multi: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
执行得很好。
我也怀疑更新是否顺利,但据我所知,使用数据库的应用程序运行良好。
解决方案
更新无法使用$
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: [ {}, {} ]
}
]
}
]
>
推荐阅读
- python-3.x - 为什么我不能在 Python 中腌制我的自定义异常
- ruby-on-rails - Sprockets::Rails::Helper::AssetNotPrecompiled 在 ResqueWeb 中
- python - 如何将权重从 Pytorch 逐层加载到 Keras?
- python - CNN 中的类权重
- python - 使用记事本打开文件时写入 utf8 不起作用
- javascript - 递归函数只返回第一个结果
- java - JPA CriteriaQuery中不需要的交叉连接在子查询中选择
- autodesk-forge - 是否有现实捕获参数来请求所需的顶点数量?
- r - R中两个栅格堆栈之间每个位置的最佳拟合线
- reactjs - react-select defaultValue 未从状态加载