MongoDB : Update Modifier semantics of “$unset”

前端 未结 1 2000
礼貌的吻别
礼貌的吻别 2021-01-03 02:53

In MongoDB, the update modifier unset works as follows:

Consider a Mongo DB Database db with a collection users. Users contain a Document, of the f

相关标签:
1条回答
  • 2021-01-03 03:11

    The short answer is because {"relationships.terminated"} is not a valid json/bson object. A JSON object is composed of a key and a value, and {"relationships.terminated"} only has a key (or value, depends on how you look it).

    Affortunately to unset a field in Mongo you do not need to set the actual value of the field you want to remove. You can use any value (1 is commonly used in Mongo docs) no matter the actual value of relationships.terminated:

    db.users.update({"username":"joe"},{"$unset":{"relationships.terminated" : 1}});
    
    0 讨论(0)
提交回复
热议问题