2014-05-10 4 views
0

몽구스를 사용하여 역동적 인 조건을 만들려고하고 있지만 상상하지 못합니다. 당신이, 내가 "인덱스"변수를 사용하여 동적 조건을 만들려고 노력하고있다시피몽구스로 다이나믹 한 쿼리하기

코드는이

id = req.params.questionId; 
index = req.params.index; 

callback = function(err,value){ 
    if(err){ 

     res.send(err) 

    }else{ 

     res.send(value) 

    } 
}; 

conditions = { 
    "_id": id, 
    "array._id": filterId 
}; 
updates = { 
    $push: { 
     "array.$.array2."+index+".answeredBy": userId 
    } 
}; 
options = { 
    upsert: true 
}; 

Model.update(conditions, updates, options, callback); 

같다. 이럴 수 있니?

미리 감사드립니다.

답변

4

은 두 단계로 updates 객체를 생성해야합니다

: 이제 4+가 computed property names, 당신은 한 번에 수행 할 수 있습니다 지원 Node.js를

var updates = { $push: {} }; 
updates.$push["array.$.array2." + index + ".answeredBy"] = userId; 

업데이트

var updates = { $push: { ["array.$.array2." + index + ".answeredBy"]: userId } }; 
+0

또는 Object.create를 사용하여 –

+0

내 생명을 구했어. 고맙습니다! – masanorinyo