mongodb 如何删除mongodb中的数组元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16959099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove array element in mongodb?
提问by Justin John
Here is array structure
这是数组结构
contact: {
phone: [
{
number: "+1786543589455",
place: "New Jersey",
createdAt: ""
}
{
number: "+1986543589455",
place: "Houston",
createdAt: ""
}
]
}
Here I only know the mongo id(_id
) and phone number(+1786543589455
) and I need to remove that whole corresponding array element from document. i.e zero indexed element in phone array is matched with phone number and need to remove the corresponding array element.
在这里,我只知道 mongo id( _id
) 和电话号码 ( +1786543589455
),我需要从文档中删除整个相应的数组元素。即电话数组中的零索引元素与电话号码匹配,需要删除相应的数组元素。
contact: {
phone: [
{
number: "+1986543589455",
place: "Houston",
createdAt: ""
}
]
}
I tried with following update method
我尝试使用以下更新方法
collection.update(
{ _id: id, 'contact.phone': '+1786543589455' },
{ $unset: { 'contact.phone.$.number': '+1786543589455'} }
);
But it removes number: +1786543589455
from inner array object, not zero indexed element in phone array. Tried with pull
also without a success.
但它number: +1786543589455
从内部数组对象中删除 ,而不是电话数组中的零索引元素。试过pull
也没有成功。
How to remove the array element in mongodb?
如何删除mongodb中的数组元素?
回答by Leonid Beschastny
Try the following query:
尝试以下查询:
collection.update(
{ _id: id },
{ $pull: { 'contact.phone': { number: '+1786543589455' } } }
);
It will find document with the given _id
and remove the phone +1786543589455
from its contact.phone
array.
它将找到具有给定的文档_id
并+1786543589455
从其contact.phone
数组中删除电话。
You can use $unset
to unset the value in the array (set it to null
), but not to remove it completely.
您可以使用$unset
取消设置数组中的值(将其设置为null
),但不能完全删除它。
回答by chirag jain
This below code will remove the complete object element from the array, where the phone number is '+1786543589455'
下面的代码将从数组中删除完整的对象元素,其中电话号码是“+1786543589455”
db.collection.update(
{ _id: id },
{ $pull: { 'contact': { number: '+1786543589455' } } }
);
回答by Syeda Aimen Batool
You can simply use $pull to remove a sub-document. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.
您可以简单地使用 $pull 删除子文档。$pull 运算符从现有数组中删除一个或多个与指定条件匹配的值的所有实例。
Collection.update({
_id: parentDocumentId
}, {
$pull: {
subDocument: {
_id: SubDocumentId
}
}
});
This will find your parent document against given ID and then will remove the element from subDocument which matched the given criteria.
这将根据给定的 ID 找到您的父文档,然后从子文档中删除与给定条件匹配的元素。
Read more about pull here.
在此处阅读有关拉的更多信息。
回答by Md Alamin
In Mongoose: from the document:
在猫鼬中:来自文档:
To remove a document from a subdocument array we may pass an object with a matching _id.
要从子文档数组中删除文档,我们可以传递一个具有匹配 _id 的对象。
contact.phone.pull({ _id: itemId }) // remove
contact.phone.pull(itemId); // this also works
See Leonid Beschastny's answer for the correct answer.
有关正确答案,请参阅 Leonid Beschastny 的答案。