ZaonX
ZaonX

Reputation: 13

Delete a specific data from mongo

I want to delete specific data from mongo. below is my json. I want to delete all occurence of IIT-395.

{
"_id" : "i34908s",
"lifeStageCourses" : [ 
    {
        "lifeStage" : "in_school",
        "tags" : [ 
            "IIT-182"

        ],
        "courseIds" : []
    }, 
    {
        "lifeStage" : "in_college",
        "tags" : [ 
            "IIT-134", 
            "IIT-140",
            "IIT-395"
        ],
        "courseIds" : []
    }, 
    {
        "lifeStage" : "prep_entrance_exam",
        "tags" : [],
        "courseIds" : []
    }, 
    {
        "lifeStage" : "job_seeker",
        "tags" : [ 
            "IIT-134",
            "IIT-395"
        ],
        "courseIds" : []
    }
]
}

I expect output only IIT-395 should be removed

Instead whole document is getting deleted.

Upvotes: 0

Views: 77

Answers (1)

Tejal
Tejal

Reputation: 814

try following query .

db.getCollection('careerpaths').update(
    {"lifeStageCourses.tags" : "IIT-395"},
    {$pull : {
        "lifeStageCourses.$[].tags" :"IIT-395"
        }},false, true
    );

Upvotes: 1

Related Questions