【问题标题】:Delete object inside array in collection mongo db [duplicate]删除集合mongo db中数组内的对象[重复]
【发布时间】:2017-05-17 19:22:56
【问题描述】:

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

【问题讨论】:

    标签: mongodb bson


    【解决方案1】:

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

    例如:

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

    将删除第一个小部件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多