如何在mongodb中更新子文档

如何在mongodb中更新子文档

本文介绍了如何在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" : "[email protected]",
    "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.$.<field_name>更新子文档的特定字段.

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中更新子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:41