問題:
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