0
我使用這個查詢:更新嵌套的數組不工作 - MongoDB的
Model.update(
{_id: req.params.questions_id, "doc.questionSets.$._id": req.params.set_id},
{$pushAll: {"questions": req.body}},
{upsert:true},
function(err, questions){
console.log("err", err);
console.log("err", questions);
}
)
路由呼叫:
localhost:3131/api/v0.1/charting/questions/56cff03e9ff240192da2fa34/set/56cff04e9ff240192da2fa3a/add-new-question
其中:charting/questions/:questions_id/set/:set_id/add-new-question
文檔數據:
/* 1 */
{
"_id" : ObjectId("56cff03e9ff240192da2fa34"),
"questionSets" : [
{
"name" : "Physical exam questions",
"_id" : ObjectId("56cff03e9ff240192da2fa35"),
"questions" : [
{
"question" : "What is love?",
"answer" : "",
"_id" : ObjectId("56cff03e9ff240192da2fa39")
}
]
},
{
"name" : "Brain questions",
"_id" : ObjectId("56cff04e9ff240192da2fa3a"),
"questions" : [
{
"question" : "What is love?",
"answer" : "",
"_id" : ObjectId("56cff04e9ff240192da2fa3e")
}
]
}
],
"updatedAt" : ISODate("2016-02-26T06:26:39.330Z")
}
我想推此JSON對象questionSets[0].questions
:
[
{
"question" : "Added 1?"
}
]
但查詢返回【OK:0,N:0,n修改:0}和文件尚未更新。我在這裏做錯了什麼?謝謝。
嗨。它返回了這個錯誤。 '{名稱: 'MongoError', 消息: '不能沒有包含數組對應的查詢字段申請的位置操作。', 司機:真, 確定:1, N:0, 代碼:16650, ERRMSG :'不能應用位置運算符而沒有包含數組的相應查詢字段', writeConcernError: {code:16650, errmsg:'如果沒有包含數組的相應查詢字段,則無法應用位置運算符。 }} ' – CENT1PEDE
@TheGreenFoxx,對不起以前的粗心大意,是'req.body'數組的值嗎? – zangw
是的,它看起來像這樣。 '[{「question」:「Added 1?」}]' – CENT1PEDE