我正在从事 NodeJS 项目,我想删除包含对象的数组元素。
架构是:
{
"products" : [
{
"productid" : 1,
"name" : "product 1",
"price" : 200
},
{
"productid" : 2,
"name" : "product 2",
"price" : 300
},
{
"productid" : 3,
"name" : "product 3",
"price" : 350
},
{
"productid" : 4,
"name" : "product 4",
"price" : 300
},
,
{
"productid" : 5,
"name" : "product 5",
"price" : 300
}
]
}
我想删除产品3,mongodb中查询如何? “productid”属性是关键,不再重复
谢谢。
最佳答案
使用这个:
根据您的需要更改集合
名称和{}
。
db.collection.update( {}, { "$pull" : { "products" : { "productid" : 3 } } });
如果它匹配多个元素,则使用多个选项,例如
db.collection.update( {}, { "$pull" : { "products" : { "productid" : 3 } } },
{ multi: true });
关于javascript - 删除包含对象的数组 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34148165/