形式這個問題已經被問了幾次,但我一直無法找到一個解決方案:流星集合:UPSERT W /陣列
我有這樣一個模式(簡體):
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'而不是? – Styx
是的,抱歉 - 正在簡化代碼並錯過了。正在改變。 – ASX
那麼,現在使用'$'是有道理的。雖然,你的意圖還不清楚。你想添加另一個對象到'statusArray'數組或更新其中的現有對象嗎? – Styx