0
내 MongoDB의 문서는 다음과 같습니다MongoDB의 증가 수는 하위 문서에
{
valOne: "one",
valTwo: "two",
valThree: {
threeOne: 0,
threeTwo: 0
}
}
내가 "threeOne"또는 사용자의 요청에 따라 "threeTwo"중 하나를 증가하고 싶습니다. 지금까지
내 코드 :
var whichFieldToUpdate = request.body.field; //can be either threeOne or threeTwo
var id = new BSON.ObjectID(request.body.id); //contains document id
db.collection('name').update({_id: id}, {$inc: { ?????? } },
function(err, result) {
});
???? {$inc: {valThree: {whichFieldToUpdate : 1 } }
하위 문서 {$ INC에 액세스 할 점 표기법을 사용합니다 : 이런 식으로 뭔가해야한다 { 'valThree.threeOne': 1} – joao
그래 내가 들으 것을 이동합니다. 하지만 문제는 응답에 따라 threeOne이 변경되고 {$ inc : { 'valThree.' + whichFieldToUpdate : 1}가 작동하지 않는 것 같습니다. – BuildingJarl
whichFieldToUpdate를 동적으로 넣는 대신 if 문을 사용하십시오. – joao