如何刪除MongoDB中的數(shù)組元素?這是數(shù)組結(jié)構(gòu)contact: {
phone: [
{
number: "+1786543589455",
place: "New Jersey",
createdAt: ""
}
{
number: "+1986543589455",
place: "Houston",
createdAt: ""
}
]}在這里,我只知道蒙戈的身份(_id)和電話號碼(+1786543589455),我需要從文檔中刪除整個(gè)對應(yīng)的數(shù)組元素。也就是說,電話數(shù)組中的零索引元素與電話號碼匹配,需要?jiǎng)h除相應(yīng)的數(shù)組元素。contact: {
phone: [
{
number: "+1986543589455",
place: "Houston",
createdAt: ""
}
]}我嘗試使用以下更新方法collection.update(
{ _id: id, 'contact.phone': '+1786543589455' },
{ $unset: { 'contact.phone.$.number': '+1786543589455'} });但它消除了number: +1786543589455從內(nèi)部數(shù)組對象,而不是電話數(shù)組中的零索引元素。試著pull也沒有成功。如何刪除MongoDB中的數(shù)組元素?
如何刪除MongoDB中的數(shù)組元素?
呼喚遠(yuǎn)方
2019-07-02 17:22:00