0

I have a collection name dbapp in my mongodb. There is a document as follows,I want to remove on widget object inside widgets.I am troubling to create mongodb command script for that.Apprciate your help.thanks

  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"
        }
    });
Sandun Priyanka
  • 591
  • 7
  • 24

1 Answers1

-1

You may want to use the operator $pull. https://docs.mongodb.com/manual/reference/operator/update/pull/

For example:

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

will remove the first widget

bappr
  • 175
  • 2
  • 9