[英]How to remove “attribute” on array in embedded document in MongoDb?
I need to remove every attribute called "cast_id" from this document: 我需要从此文档中删除每个称为“ cast_id”的属性:
{
"_id": ObjectId("5b895f15674e2ddf78bcc6f8"),
"cast": [
{
'cast_id': 14,
'character': 'Woody (voice)',
'credit_id': '52fe4284c3a36847f8024f95',
'gender': 2,
'id': 31,
'name': 'Tom Hanks',
'order': 0,
'profile_path': '/pQFoyx7rp09CJTAb932F2g8Nlho.jpg'
},
{
'cast_id': 15,
'character': 'Buzz Lightyear (voice)',
'credit_id': '52fe4284c3a36847f8024f99',
'gender': 2,
'id': 12898,
'name': 'Tim Allen',
'order': 1,
'profile_path': '/uX2xVf6pMmPepxnvFWyBtjexzgY.jpg'
},
{
'cast_id': 16,
'character': 'Mr. Potato Head (voice)',
'credit_id': '52fe4284c3a36847f8024f9d',
'gender': 2,
'id': 7167,
'name': 'Don Rickles',
'order': 2,
'profile_path': '/h5BcaDMPRVLHLDzbQavec4xfSdt.jpg'
},
{
'cast_id': 17,
'character': 'Slinky Dog (voice)',
'credit_id': '52fe4284c3a36847f8024fa1',
'gender': 2,
'id': 12899,
'name': 'Jim Varney',
'order': 3,
'profile_path': '/eIo2jVVXYgjDtaHoF19Ll9vtW7h.jpg'
},
{
'cast_id': 18,
'character': 'Rex (voice)',
'credit_id': '52fe4284c3a36847f8024fa5',
'gender': 2,
'id': 12900,
'name': 'Wallace Shawn',
'order': 4,
'profile_path': '/oGE6JqPP2xH4tNORKNqxbNPYi7u.jpg'
},
{
'cast_id': 19,
'character': 'Hamm (voice)',
'credit_id': '52fe4284c3a36847f8024fa9',
'gender': 2,
'id': 7907,
'name': 'John Ratzenberger',
'order': 5,
'profile_path': '/yGechiKWL6TJDfVE2KPSJYqdMsY.jpg'
},
{
'cast_id': 20,
'character': 'Bo Peep (voice)',
'credit_id': '52fe4284c3a36847f8024fad',
'gender': 1,
'id': 8873,
'name': 'Annie Potts',
'order': 6,
'profile_path': '/eryXT84RL41jHSJcMy4kS3u9y6w.jpg'
},
{
'cast_id': 26,
'character': 'Andy (voice)',
'credit_id': '52fe4284c3a36847f8024fc1',
'gender': 0,
'id': 1116442,
'name': 'John Morris',
'order': 7,
'profile_path': '/vYGyvK4LzeaUCoNSHtsuqJUY15M.jpg'
},
{
'cast_id': 22,
'character': 'Sid (voice)',
'credit_id': '52fe4284c3a36847f8024fb1',
'gender': 2,
'id': 12901,
'name': 'Erik von Detten',
'order': 8,
'profile_path': '/twnF1ZaJ1FUNUuo6xLXwcxjayBE.jpg'
},
{
'cast_id': 23,
'character': 'Mrs. Davis (voice)',
'credit_id': '52fe4284c3a36847f8024fb5',
'gender': 1,
'id': 12133,
'name': 'Laurie Metcalf',
'order': 9,
'profile_path': '/unMMIT60eoBM2sN2nyR7EZ2BvvD.jpg'
},
{
'cast_id': 24,
'character': 'Sergeant (voice)',
'credit_id': '52fe4284c3a36847f8024fb9',
'gender': 2,
'id': 8655,
'name': 'R. Lee Ermey',
'order': 10,
'profile_path': '/r8GBqFBjypLUP9VVqDqfZ7wYbSs.jpg'
},
{
'cast_id': 25,
'character': 'Hannah (voice)',
'credit_id': '52fe4284c3a36847f8024fbd',
'gender': 1,
'id': 12903,
'name': 'Sarah Freeman',
'order': 11,
'profile_path': None
},
{
'cast_id': 27,
'character': 'TV Announcer (voice)',
'credit_id': '52fe4284c3a36847f8024fc5',
'gender': 2,
'id': 37221,
'name': 'Penn Jillette',
'order': 12,
'profile_path': '/zmAaXUdx12NRsssgHbk1T31j2x9.jpg'
}
]
}
This document contains at least 45000 item like that, and I have to remove from each one, the "attribute" "cast_id" and more "attribute", like "gender" or "profile_path" . 该文档至少包含45000个这样的项目,我必须从每个项目中删除“属性”, “ cast_id”以及更多“属性”,例如“ gender”或“ profile_path” 。
I try to use $pull , $pullAll , $unset , and remove in this way, but unsuccessfully. 我尝试使用$ pull , $ pullAll , $ unset和以这种方式删除 ,但未成功。 ( "crediti" is the name of my collection)
( “ crediti”是我收藏的名称)
db.crediti.update({}, {$pull: {cast: {cast_id}}}, {multi:true})
db.crediti.update({}, {$unset: {cast: {cast_id}}}, {multi:true})
db.crediti.update({}, {$pullAll: {cast: {cast_id}}}, {multi:true})
db.crediti.remove({"cast.cast_id"})
Otherwise, I try to use $unset in this way: 否则,我尝试以这种方式使用$ unset :
db.crediti.update({}, {$unset:{"cast.cast_id":1}},{multi:true})
And the result is: 结果是:
WriteResult({ "nMatched" : 45476, "nUpserted" : 0, "nModified" : 0 })
They matched, but weren't modified. 他们匹配,但未修改。
The result that I want, is to maintain other attribute, like this: 我想要的结果是维护其他属性,如下所示:
{
"_id" : ObjectId("5b895f15674e2ddf78bcc6f8"),
"cast" : "[{'character': 'Woody (voice)', 'id': 31, 'name': 'Tom Hanks'}, etc...
How can I do this? 我怎样才能做到这一点?
we can do this using $unset and filtered positional operator 我们可以使用$ unset和过滤后的位置运算符
the update to remove all the cast_id
from the array is shown below 下面显示了从数组中删除所有
cast_id
的更新
db.crediti.update(
{},
{$unset:{"cast.$[].cast_id":""}},
{multi:1}
);
Update query has three parts, 更新查询包括三个部分,
The first part is to pick all the documents in the collection which we are going to update, here we have given it as empty document so it applies to all documents in the collection. 第一部分是选择要更新的集合中的所有文档,这里我们将其指定为空文档,以便将其应用于集合中的所有文档。
The second part of the query is using filtered positional operator together with $unset, which does the job of removing the cast_id
from all documents inside the array. 查询的第二部分将使用过滤后的位置运算符和$ unset一起使用,该操作
cast_id
从数组内的所有文档中删除cast_id
。
The third part of the query does the update for multiple documents on the collection 查询的第三部分对集合中的多个文档进行更新
Please note that $[identifier] works from version 3.6 请注意, $ [identifier]适用于3.6版
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.