如何更新mongodb中的子文档 [英] How to update a subdocument in mongodb

查看:152
本文介绍了如何更新mongodb中的子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过很多次了,但我不知道如何在 mongo 中更新子文档.

I know the question have been asked many times, but I can't figure out how to update a subdocument in mongo.

这是我的架构:

// Schemas
var ContactSchema = new mongoose.Schema({
  first: String,
  last: String,
  mobile: String,
  home: String,
  office: String,
  email: String,
  company: String,
  description: String,
  keywords: []
});

var UserSchema = new mongoose.Schema({
  email: {
      type: String,
      unique: true,
      required: true
  },
  password: {
      type: String,
      required: true
  },
  contacts: [ContactSchema]
});

我的收藏是这样的:

db.users.find({}).pretty()
{
    "_id" : ObjectId("5500b5b8908520754a8c2420"),
    "email" : "test@random.org",
    "password" :     "$2a$08$iqSTgtW27TLeBSUkqIV1SeyMyXlnbj/qavRWhIKn3O2qfHOybN9uu",
    "__v" : 8,
    "contacts" : [
        {
            "first" : "Jessica",
            "last" : "Vento",
            "_id" : ObjectId("550199b1fe544adf50bc291d"),
            "keywords" : [ ]
        },
        {
            "first" : "Tintin",
            "last" : "Milou",
            "_id" : ObjectId("550199c6fe544adf50bc291e"),
            "keywords" : [ ]
        }
    ]
}

假设我想通过以下方式更新 id 为 550199c6fe544adf50bc291e 的子文档:

Say I want to update subdocument of id 550199c6fe544adf50bc291e by doing:

db.users.update({_id: ObjectId("5500b5b8908520754a8c2420"), "contacts._id": ObjectId("550199c6fe544adf50bc291e")}, myNewDocument)

与 myNewDocument 类似:

with myNewDocument like:

{ "_id" : ObjectId("550199b1fe544adf50bc291d"), "first" : "test" }

它返回一个错误:

db.users.update({_id: ObjectId("5500b5b8908520754a8c2420"), "contacts._id":     ObjectId("550199c6fe544adf50bc291e")}, myNewdocument)
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 0,
    "nModified" : 0,
    "writeError" : {
        "code" : 16837,
        "errmsg" : "The _id field cannot be changed from {_id:     ObjectId('5500b5b8908520754a8c2420')} to {_id:     ObjectId('550199b1fe544adf50bc291d')}."
    }
})

我知道 mongo 试图替换父文档而不是子文档,但最后,我不知道如何更新我的子文档.

I understand that mongo tries to replace the parent document and not the subdocument, but in the end, I don't know how to update my subdocument.

推荐答案

您需要使用 $ 运算符 更新数组中的子文档

You need to use the $ operator to update a subdocument in an array

使用contacts.$会指向mongoDB更新相关的子文档.

Using contacts.$ will point mongoDB to update the relevant subdocument.

db.users.update({_id: ObjectId("5500b5b8908520754a8c2420"), 
  "contacts._id": ObjectId("550199c6fe544adf50bc291e")}, 
 {"$set":{"contacts.$":myNewDocument}})

我不确定您为什么要更改子文档的 _id.这是不可取的.

I am not sure why you are changing the _id of the subdocument. That is not advisable.

如果您想更改子文档的特定字段,请使用 contacts.$. 更新子文档的特定字段.

If you want to change a particular field of the subdocument use the contacts.$.<field_name> to update the particular field of the subdocument.

这篇关于如何更新mongodb中的子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆