[英]How can I pull sub-documents from an array?
I have this document 我有这个文件
{
"_id" : ObjectId("56877d72572434211f8c579e"),
"hola" : {
"nombres" : [
"andres",
"jose"
]
},
"id" : "8888",
"aloh" : [
{
"saludo" : [
{
"qwe" : "rty",
"sad" : "fet"
},
{
"dvo" : "rak",
"foo" : "foo"
}
]
},
{
"despedida" : "bye"
}
]
}
And I want to delete just the object that contains 我只想删除包含
{
"qwe" : "rty",
"sad" : "fet"
}
I'm trying using $pull and $elemMatch like this: 我正在尝试使用$ pull和$ elemMatch这样的:
db.collection.update(
{ "id":"8888" },
{ "$pull": { "aloh": { "saludo": { "$elemMatch": { "qwe": "rty" } } } } }
)
But it eliminates all the parent array and I want it to output something like this: 但是它消除了所有父数组,我希望它输出如下内容:
{
"_id" : ObjectId("56877d72572434211f8c579e"),
"hola" : {
"nombres" : [
"andres",
"jose"
]
},
"id" : "8888",
"aloh" : [
{
"saludo" : [
{
"dvo" : "rak",
"foo" : "foo"
}
]
},
{
"despedida" : "bye"
}
]
}
Following query will work 以下查询将起作用
db.collection.update(
{ "id":"8888" },
{
$pull: {
"aloh.0.saludo": {
"qwe": "rty",
"sad": "fet"
}
}
});
It's in situation like this that you use the positional $
update operator. 在这种情况下,您将使用位置$
update运算符。 One thing to note is that the array field must appear as part of the query document. 需要注意的一件事是,数组字段必须作为查询文档的一部分出现。 That is what explain the use of $exists
here. 这就是在这里解释$exists
的用法的原因。
db.collection.update(
{ "id": "8888", "aloh.saludo": { "$exists": true } },
{ "$pull": { "aloh.$.saludo": { "qwe": "rty", "sad": "fet" } } }
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.