【发布时间】:2018-03-05 05:15:24
【问题描述】:
这个问题的形式已被问过几次,但我一直无法找到解决方案:
我有这样的架构(简化):
StatusObject = new SimpleSchema({
statusArray: [statusSchema]
});
statusSchema 在哪里
{
topicId:{
type: String,
optional: true
},
someInfo:{
type: Number,
optional: true,
decimal: true
},
otherInfo:{
type: Number,
optional: true
}
}
我正在尝试upsert - 使用以下流星方法代码:
var upsertResult = BasicInfo.update({
userId: this.userId,
statusArray: {
$elemMatch: { topicId : newStatus.topicId }
}
}, {
$set: {
"statusArray.$.topicId": newStatus.topicId,
"statusArray.$.someInfo": newStatus.someInfo,
"statusArray.$.otherInfo": newStatus.otherInfo
}
}, {
multi: true,
upsert: true
});
但我不断收到错误消息:statusArray must be an array
我想通过添加$,我确保它被识别为一个数组?我错过了什么?
【问题讨论】:
-
您查询中的
topicCompletion字段是什么?你的意思是statusArray吗? -
是的,抱歉 - 正在简化代码并错过了这一点。现在改变。
-
好吧,现在使用
$是合理的。虽然,你的意图还不清楚。您想将另一个对象添加到statusArray数组中还是更新其中的现有对象? -
我想在数组中有一个 topicId=x 的对象。 (即 topicId 字段在所有数组元素中应该是唯一的)。然后我想更新该数组元素的 someInfo 和 otherInfo
-
感谢您澄清这一点。最后一个问题:如果没有
statusArray.topicId匹配newStatus.topicId的文档?创建新文档还是将新对象推送到statusArray数组中?
标签: meteor mongodb-query