这里是数组 struct
contact: {
phone: [
{
number: "+1786543589455",
place: "New Jersey",
createdAt: ""
}
{
number: "+1986543589455",
place: "Houston",
createdAt: ""
}
]
}
这里我只知道mongo id(_id
)和电话号码(+1786543589455
),我需要从文档中删除整个对应的数组元素.i、 电话数组中的e zero索引元素与电话号码匹配,需要删除相应的数组元素.
contact: {
phone: [
{
number: "+1986543589455",
place: "Houston",
createdAt: ""
}
]
}
我try 了以下更新方法
collection.update(
{ _id: id, 'contact.phone': '+1786543589455' },
{ $unset: { 'contact.phone.$.number': '+1786543589455'} }
);
但它从内部数组对象中移除number: +1786543589455
,而不是phone数组中的零索引元素.用pull
也没成功.
如何删除mongodb中的数组元素?