1

I'm trying to remove from MongoDB collection an object (not document) that meets specific value condition, in this case - "Accessible" : "null" - while keeping other instances of this objects. I tried db.collection.update({}, {$unset: { "Accessible":"null"}}, false, true) but it removed all objects with "Accessible" key.Thanks in advance My MongoDB collection before update

{
"_id" : ObjectId("52e5f09e8f3d99e1046abccc"),
    "Name" : "Skyline",
"Accessible" : "Y"

}
{
"_id" : ObjectId("52e5f09e8f3d99e1046abccd"),
    "Name" : "Highland",
"Accessible" : "null"

}

Desired result:

{
"_id" : ObjectId("52e5f09e8f3d99e1046abccc"),
    "Name" : "Skyline",
"Accessible" : "Y"

}
{
"_id" : ObjectId("52e5f09e8f3d99e1046abccd"),
    "Name" : "Highland"

}

1 Answer 1

1

You need to first identify the documents you wish to update and then unset that specific field:

db.collection.update(
    {"Accessible" : "null"}, 
    {$unset: { "Accessible" : ""}},
    { multi: true }
)

Further documentation on $unset operator: http://docs.mongodb.org/manual/reference/operator/update/unset/

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

1 Comment

Please mark answer as correct, if you feel it was :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.