[英]In-/upserting a nested document in MongoDB
I'm trying to upsert a nested document (activities) in a structure similar to 我正在尝试以类似于以下内容的结构来插入嵌套文档(活动)
{
"_id" : "123",
"modules" : {
"x" : {
},
"y" : {
"activities" : {
"preview": {
"createdAt": "2014-10-13 15:21:22.113",
"data": {}
}
}
},
"z" : {
"activities" : {
"render": {
"createdAt": "2014-10-15 04:22:25.171",
"data": {}
},
"render": {
"createdAt": "2014-10-14 02:42:24.132",
"data": {}
}
}
}
}
}
I then try 然后我尝试
selector = { "_id": id, "modules": module, "activities" }
activity = { "preview": { "data": data }}
Meteor.users.upsert(selector, { $set: activity, $setOnInsert: { "createdAt: new Date()" }})
Which yields 哪个产量
MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: meteor.users.$_id_ dup key: { : "123" }
_id
is a unique index key. _id
是唯一的索引键。 There are additional fields/documents on the _id
'level' of each user and on the activities
'level' of each module, but none concerning the upsert. 每个用户的
_id
“级别”上以及每个模块的activities
“级别”上还有其他字段/文档,但没有关于upsert的字段/文档。 Each module is unique. 每个模块都是唯一的。 The activities document doesn't exist before the first record is inserted.
在插入第一个记录之前,活动文档不存在。
With that, I hope someone gets the gist of what I'm trying to accomplish and can help me in the right direction. 借此,我希望有人能掌握我要完成的目标,并能向正确的方向提供帮助。
Upsert is for insert or update(if exist) a new document. Upsert用于插入或更新(如果存在)新文档。 It does not apply for a sub-document.
它不适用于子文档。
You should just update your document: 您应该只更新文档:
selector = { "_id": id, "modules": module, preview : null }
activity = { "preview": { "data": data , "createdAt" : new Date() }}
Meteor.users.update(selector, { $set: 'activity' : activity }})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.