Make dynamic query with Mongoose

前端 未结 1 1751
耶瑟儿~
耶瑟儿~ 2020-12-12 08:00

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 =          


        
相关标签:
1条回答
  • 2020-12-12 08:20

    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
    } };
    
    0 讨论(0)
提交回复
热议问题