【发布时间】:2020-12-20 13:35:06
【问题描述】:
这是我的数据结构:
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":""}})
如果我通过multi:true,这将很好地删除matches 的每个元素中的整个数组otherField(为了便于阅读,在此处删除)。
但是当我尝试时:
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 array in mongodb 得到了这个答案,并将我的 mongo 版本从 3.4 更新到 3.6,因为 $[] 是在 3.6 中引入的,正如 svr 所说,并在 docs 中显示。
未设置有效,但路径似乎是我更新的错误部分。
"matches.$.goals" 返回错误,matches.[].goals 也没有任何影响。
我什至想知道是否可能有一些缓存,但这没有多大意义,因为otherField 上的未设置执行得很好。
我也怀疑更新是否顺利,但据我所知,使用数据库的应用程序运行良好。
【问题讨论】: