Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a Document, I want to use $pull or other good way to remove only device where "mobile_id": "opr_134" from devices list of address.

{
    "user_id": "eamn",
    "address": [{
        "address_id": 32,
        "city_name": "DP 1000"
        "devices": [{
            "mobile_id": "opr_123",
            "created_at": 2132,
            "device": "company"
        },{
            "mobile_id": "opr_134",
            "created_at": 2134,
            "device": "company2"
        }]
    }]
}

Please suggest an atomic way to remove this device.

question from:https://stackoverflow.com/questions/66050540/mongodb-remove-3rd-level-embedded-document

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
74 views
Welcome To Ask or Share your Answers For Others

1 Answer

This can do the job:

 db.triLevel.update({},{$pull:{"address.$[].devices":{"mobile_id":"opr_134"}}})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...