I have a model userDatas and it contain a list of user data.
[{
"_id" : ObjectId("5bb6730721f28a295436b36f"),
"userId" : "5bb6730721f28a295436b36e",
"reputationNumber" : 0,
"questions" : [],
"answers" : []
},
{
"_id" : ObjectId("5bb6738c21f28a295436b371"),
"userId" : "5bb6738c21f28a295436b370",
"reputationNumber" : 0,
"questions" : [],
"answers" : []
}]
I want to filter by userId and add "5bb7d72af050ca0910282ff4" string to questions array. How to accomplish that?
//Userdatas.find
Since you are using mongoose you can use the findOneAndUpdate and addToSet to achieve this:
Userdatas.findOneAndUpdate(
{userId: "5bb7d72af050ca0910282ff4"},
{$addToSet: {questions: '5bb7d72af050ca0910282ff4'}},
function (err, result) {
...
}
)