문제

I am trying to make a dynamic condition using Mongoose but it doesn't work as I imagined.

the code is like this

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);

As you see, I am trying to make dynamic condition using "index" variable. Is it possible to do like this?

Thank you in advance!

도움이 되었습니까?

해결책

You need to create your updates object in two steps:

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

Update

Now that node.js 4+ supports computed property names, you can do this in one step:

var updates = { $push: {
    ["array.$.array2." + index + ".answeredBy"]: userId
} };
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top