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

1 Answer 1

-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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.