删除包含对象MongoDB的数组
我在一个项目oj NodeJS工作,我想删除包含objets的数组元素。
Schema是:
{ "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”属性是关键,不重复
谢谢。
用这个:
根据需要更改collection
名称和{}
。
db.collection.update( {}, { "$pull" : { "products" : { "productid" : 3 } } });
如果它匹配多个元素,然后使用多选项
db.collection.update( {}, { "$pull" : { "products" : { "productid" : 3 } } }, { multi: true });
db.collection.update( {<query>}, { "$pull" : { "products.productid" : 3 } });