问题:
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.在这里,我只知道 mongo id( _id
) 和电话号码( +1786543589455
),我需要从文档中删除整个相应的数组元素。 ie zero indexed element in phone array is matched with phone number and need to remove the corresponding array element.即电话数组中的零索引元素与电话号码匹配,需要删除相应的数组元素。
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.但它从内部数组对象中删除了number: +1786543589455
,而不是 phone 数组中的零索引元素。 Tried with pull
also without a success.尝试pull
也没有成功。
How to remove the array element in mongodb?如何删除mongodb中的数组元素?
解决方案:
参考一: https://en.stackoom.com/question/199pr参考二: https://stackoom.com/question/199pr