[英]How to delete the field in the mongodb collection?
I have a data collection in mongodb like below: 我在mongodb中有一个数据收集,如下所示:
{
{ "_id" : ObjectId("1"), "name" : "ABC", "group" : [ObjectId("11"), ObjectId("12"), ObjectId("13")]}
{ "_id" : ObjectId("2"), "name" : "DEF", "group" : [ObjectId("21"), ObjectId("22"), ObjectId("23")]}
}
I want to delete ObjectId("11")
in the group
field in document ObjectId("1")
. 我想在文档ObjectId("1")
的group
字段中删除ObjectId("11")
ObjectId("1")
。
I tried the code below: 我尝试了下面的代码:
aId = "1"
bId = "11"
db.collection.updateOne({ _id: ObjectId(aId) }, { $pull: { group: { _id: ObjectId(bId) } } })
But failed. 但是失败了。
I have also tried: 我也尝试过:
aId = "1"
bId = "11"
db.collection.updateOne({ _id: aId }, { $pull: { group: { _id: bId } } })
But still failed to delete it. 但是仍然无法删除它。
Anything wrong with my code? 我的代码有问题吗?
Use $unset
like this: 像这样使用$unset
:
db.products.update(
{ sku: "unknown" },
{ $unset: { quantity: "", instock: "" } })
This code will remove the fields quantity and instock from the firstdocument in the products collection where the field sku has a value of unknown. 此代码将从sku字段值为unknown的产品集合中的第一个文档中删除字段数量和库存。 For more info 欲了解更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.