我试图在将要设置的MongoDB上运行脚本teacher_ids[] = [document.owner_id]
。owner_id
在集合中的所有对象上已经存在。我当前的尝试如下所示:
db.getCollection('readings').update({ $where: "this.teacher_ids[0] != this.owner_id" }, { $set: { "teacher_ids": [this.owner_id] }}, { multi: true })
一切都差不多,除了文档看起来像
doc: {
owner_id: "some_id",
teacher_ids: [undefined]
}
这必须表示this
在的第二个参数中update
未引用该文档。如何设置它:
teacher_ids = [document.owner_id]
范例文件
{
"_id" : ObjectId("586f07bd6a2169fdffb0563b"),
"teacher_ids" : [],
"owner_id" : "57c268dd6c9fd2320035e67e",
"source" : "http://www.omfgdogs.com",
"title" : "Test",
"updatedAt" : ISODate("2017-01-28T07:43:46.597Z"),
"createdAt" : ISODate("2017-01-06T02:58:05.699Z")
}
我认为在更新组件中您无法通过此方法或其他方式引用任何其他字段值。
虽然可以通过脚本来完成
db.getCollection('readings').find({ $where: "this.teacher_ids[0] != this.owner_id" }).forEach(function(x){x.teacher_ids = [x.owner_id]; db.readings.save(x)});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句