如何从MongoDB文档中删除嵌套对象的属性? [英] How to remove property of nested object from MongoDB document?
本文介绍了如何从MongoDB文档中删除嵌套对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的MongoDB文档:
I have a MongoDB document like this :
{
"_id": ObjectId("5589044a7019e802d3e9dbc5"),
"sessionId": LUUID("f49d4280-ced0-9246-a3c9-a63e68e1ed45"),
"teamId": LUUID("6ef7d1a8-f842-a54c-bd8c-daf6481f9cfc"),
"variableId": LUUID("59d1b512-eee2-6c4b-a5b5-dda546872f55"),
"values": {
"725400": 691.0000000000000000,
"725760": 686.0000000000000000,
"726120": 683.0000000000000000,
"726480": 681.0000000000000000,
"726840": 679.0000000000000000,
"727200": 678.0000000000000000,
"727560": 677.0000000000000000,
"727920": 676.0000000000000000
},
"variableType": 2,
"isSet": false,
"teamNumber": 2,
"simPageIds": []
}
我有一种情况,我必须从文档的"values"
属性中删除一个特定的属性.例如,我想从"values"
属性中删除值"727920"
.
I have a scenario that I have to delete a particular property from the "values"
property of my document. for example, I want to delete value "727920"
from the "values"
property.
由于"Values"
不是数组,因此在这里不能使用$pull
.我需要删除
Since "Values"
is not an array, I can't use $pull
here. What I need is to remove
"727920" : 676.0000000000000000
.
正确的做法是什么?
推荐答案
使用 $ unset 如下:
db.collectionName.update({},{"$unset":{"values.727920":""}})
编辑 要更新多个文档,请使用更新选项:
EDIT For updating multiple documents use update options like :
db.collectionName.update({},{"$unset":{"values.727920":""}},{"multi":true})
这篇关于如何从MongoDB文档中删除嵌套对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文