1
以下是架構: -貓鼬創建新的對象ID,同時更新內部陣列文檔
Domain.add({
domainName: {type: Types.Text, required: true, initial: true, index: true},
labconfigs :{type: Types.Relationship, ref: 'LabConfig',required: false,many: true},
});
Domain.schema.add({
userlevels: {
type: [{
labId : String,
userlevel: String
}]
}
});
這是我的示例數據庫,Robomongo輸出
Domain
{
"_id" : ObjectId("5900dbdb4bc4c2f82400198c"),
"domainName" : "ABC.com",
"userlevels" : [
{
"labId" : "104",
"userlevel" : "true"
},
{
"labId" : "401",
"userlevel" : "false"
},
{
"labId" : "202",
"userlevel" : "true"
},
{
"labId" : "102",
"userlevel" : "true"
}
],
"labconfigs" : [
ObjectId("5900dbdb4bc4c2f824001986"),
ObjectId("5900dbdb4bc4c2f824001987"),
ObjectId("5900dbdb4bc4c2f824001988"),
ObjectId("5900dbdb4bc4c2f824001989")
],
"__v" : 1
}
所以我要做的就是簡單地更新我的userlevels文件字段(Userlevels字段是JSON對象的數組)。
更新我userlevels場,所以我給了與$此以下的貓鼬功能設置labPublishedIDs的
Domain.model.update({_id: req.params.id}, {$set: {"userlevels": userauthlevel.labpublishedIDs}}, function(err, result) {
if (err) return res.apiError('database error', err);
值爲: -
{ labpublishedIDs:
[ { labId: '102', userlevel: 'false' },
{ labId: '104', userlevel: 'true' },
{ labId: '202', userlevel: 'false' },
{ labId: '401', userlevel: 'true' } ] }
但現在我的更新功能後,它會創建一個新的與每個數組對象的ObjectId。 我無法理解原因。也許值更新中更改&它創建新的ObjectId。任何原因爲什麼?
這裏是更新功能後,樣品輸出
/* 5 */
{
"_id" : ObjectId("5900dbdb4bc4c2f82400198d"),
"domainName" : "ABC.com",
"userlevels" : [
{
"labId" : "102",
"userlevel" : "false",
"_id" : ObjectId("5900de7ddcbe0a29fd7aaccb")
},
{
"labId" : "104",
"userlevel" : "true",
"_id" : ObjectId("5900de7ddcbe0a29fd7aacca")
},
{
"labId" : "202",
"userlevel" : "false",
"_id" : ObjectId("5900de7ddcbe0a29fd7aacc9")
},
{
"labId" : "401",
"userlevel" : "true",
"_id" : ObjectId("5900de7ddcbe0a29fd7aacc8")
}
],
"labconfigs" : [
ObjectId("5900dbdb4bc4c2f824001986"),
ObjectId("5900dbdb4bc4c2f824001987"),
ObjectId("5900dbdb4bc4c2f824001988"),
ObjectId("5900dbdb4bc4c2f824001989")
],
"__v" : 1
}