删除集合mongo db中数组内的对象

时间:2017-05-17 11:06:29

标签: mongodb bson

我的mongodb中有一个集合名称dbapp。有一个文件如下,我想删除小部件内的小部件对象。我很麻烦为它创建mongodb命令脚本。请帮助你。帮助

  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"
        }
    });

1 个答案:

答案 0 :(得分:-1)

您可能想要使用运算符$ pull。 https://docs.mongodb.com/manual/reference/operator/update/pull/

例如:

db.dbapp.update(
    { "id": 0 },
    { $pull: { widgets: { position: 1 } } }
)

将删除第一个小部件

相关问题