1

Mongo newb. I'm trying to remove a tag based on the key which is an ObjectID:

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

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

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

Stephane
  • 1,613
  • 5
  • 20
  • 40

1 Answers1

1

You're looking for $unset:

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

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

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

Max Noel
  • 8,810
  • 1
  • 27
  • 35