如何更新 mongodb 中的子文档
How to update a subdocument in mongodb
我知道这个问题已经被问过很多次了,但我不知道如何更新 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]
});
我的 collection 看起来像这样:
db.users.find({}).pretty()
{
"_id" : ObjectId("5500b5b8908520754a8c2420"),
"email" : "test@random.org",
"password" : "a$iqSTgtW27TLeBSUkqIV1SeyMyXlnbj/qavRWhIKn3O2qfHOybN9uu",
"__v" : 8,
"contacts" : [
{
"first" : "Jessica",
"last" : "Vento",
"_id" : ObjectId("550199b1fe544adf50bc291d"),
"keywords" : [ ]
},
{
"first" : "Tintin",
"last" : "Milou",
"_id" : ObjectId("550199c6fe544adf50bc291e"),
"keywords" : [ ]
}
]
}
假设我想更新 id 为 550199c6fe544adf50bc291e 的子文档:
db.users.update({_id: ObjectId("5500b5b8908520754a8c2420"), "contacts._id": ObjectId("550199c6fe544adf50bc291e")}, myNewDocument)
像这样使用 myNewDocument:
{ "_id" : ObjectId("550199b1fe544adf50bc291d"), "first" : "test" }
它returns一个错误:
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 试图替换 parent 文档而不是子文档,但最后,我不知道如何更新我的子文档。
您需要使用 $ operator 更新数组中的子文档
使用contacts.$
将指向mongoDB更新相关子文档。
db.users.update({_id: ObjectId("5500b5b8908520754a8c2420"),
"contacts._id": ObjectId("550199c6fe544adf50bc291e")},
{"$set":{"contacts.$":myNewDocument}})
我不确定您为什么要更改子文档的 _id
。这是不可取的。
如果要更改子文档的特定字段,请使用 contacts.$.<field_name>
更新子文档的特定字段。
我知道这个问题已经被问过很多次了,但我不知道如何更新 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]
});
我的 collection 看起来像这样:
db.users.find({}).pretty()
{
"_id" : ObjectId("5500b5b8908520754a8c2420"),
"email" : "test@random.org",
"password" : "a$iqSTgtW27TLeBSUkqIV1SeyMyXlnbj/qavRWhIKn3O2qfHOybN9uu",
"__v" : 8,
"contacts" : [
{
"first" : "Jessica",
"last" : "Vento",
"_id" : ObjectId("550199b1fe544adf50bc291d"),
"keywords" : [ ]
},
{
"first" : "Tintin",
"last" : "Milou",
"_id" : ObjectId("550199c6fe544adf50bc291e"),
"keywords" : [ ]
}
]
}
假设我想更新 id 为 550199c6fe544adf50bc291e 的子文档:
db.users.update({_id: ObjectId("5500b5b8908520754a8c2420"), "contacts._id": ObjectId("550199c6fe544adf50bc291e")}, myNewDocument)
像这样使用 myNewDocument:
{ "_id" : ObjectId("550199b1fe544adf50bc291d"), "first" : "test" }
它returns一个错误:
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 试图替换 parent 文档而不是子文档,但最后,我不知道如何更新我的子文档。
您需要使用 $ operator 更新数组中的子文档
使用contacts.$
将指向mongoDB更新相关子文档。
db.users.update({_id: ObjectId("5500b5b8908520754a8c2420"),
"contacts._id": ObjectId("550199c6fe544adf50bc291e")},
{"$set":{"contacts.$":myNewDocument}})
我不确定您为什么要更改子文档的 _id
。这是不可取的。
如果要更改子文档的特定字段,请使用 contacts.$.<field_name>
更新子文档的特定字段。