Mongodb 删除集合mongo db中数组内的对象
我的mongodb中有一个集合名dbapp。下面是一个文档,我想删除widget中的widget对象。我很难创建mongodb命令脚本。感谢您的帮助。谢谢Mongodb 删除集合mongo db中数组内的对象,mongodb,bson,Mongodb,Bson,我的mongodb中有一个集合名dbapp。下面是一个文档,我想删除widget中的widget对象。我很难创建mongodb命令脚本。感谢您的帮助。谢谢 db.dbapp.insert( { "tenantid": 16, "id": 0, "default": true, "widgets": [ { "_id": new ObjectId(),
db.dbapp.insert(
{
"tenantid": 16,
"id": 0,
"default": true,
"widgets": [
{
"_id": new ObjectId(),
"position": 1,
"type": 1,
"class": "green",
"metricid": 5
},
{
"_id": new ObjectId(),
"position": 2,
"type": 1,
"class": "blue",
"metricid": 6
},
{
"_id": new ObjectId(),
"position": 3,
"type": 2,
"class": "normal",
"metricid": 1
},
{
"_id": new ObjectId(),
"position": 4,
"type": 2,
"class": "normal",
"metricid": 2
},
{
"_id": new ObjectId(),
"position": 5,
"type": 2,
"class": "normal",
"metricid": 3
}
],
"settings": {
"appid": 0,
"appname": "default"
}
});
您可能需要使用运算符$pull。 例如:
db.dbapp.update(
{ "id": 0 },
{ $pull: { widgets: { position: 1 } } }
)
将删除第一个小部件