Refresh Mongo Array: Remove Dynamic Key

Mongo newb. I am trying to remove a tag based on a key, which is an ObjectID:

{
  "_id": ObjectId("53ccff9bbb25567911f208a8"),

  "tags": {
    "53ccff9bbb25567911f208a4": "tag1",
    "53ccff9bbb25567911f208a5": "tag2",
    "53ccff9bbb25567911f208a6": "tag3"
  }
}

I think I know how to remove it from an array in javascript and update the document, but I'm trying to do this in a request.

+1
source share
1 answer

You are looking for $unset:

collection.update(
    {"_id": ObjectId("53ccff9bbb25567911f208a8")},
    {"$unset": {"tags.53ccff9bbb25567911f208a6": ""}}
)

This will delete the entry "53ccff9bbb25567911f208a6": "tag3"from tags.

Additional information at http://docs.mongodb.org/manual/reference/operator/update/unset/#up._S_unset

+1
source

Source: https://habr.com/ru/post/1690355/


All Articles