【问题标题】:how to add and delete a sub document in MongoDB shell?如何在 MongoDB shell 中添加和删除子文档?
【发布时间】:2013-08-22 17:27:36
【问题描述】:

例如,假设我有这个文件 {name : "John", Address : { Street : "Main St", City : "Houston", State : "TX"}, email : "John@gmail.com" }

如果我想删除上述文档中的子文档“Street”,我该怎么办? 如果我想添加一个子文档“Zip”。

db.collection.remove()

顺便问一下,db.collection.find() 中是否有任何通配符来匹配字段名称?

【问题讨论】:

    标签: mongodb document


    【解决方案1】:

    您正在寻找$set$unset。你可以这样使用它:

    db.collection.update({name: "John"}, {$unset: {"address.street": ""}, $set: {"address.zip": 94105}})
    

    MongoDB 中没有通配符字段名称匹配器。我预计未来不会有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2014-07-29
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多