1

I want to pass a variable to the query, but I didn't find out how:

 db.collection('users').update(
  { "_id":ObjectID(req.params.id)},
  { $set:  { tasks[req.params.pos].done : true }}
  ,(error,resultat)=>{
       if (resultat){
         console.log("mise a jour avec succes ");
         res.send({message:"mise a jour avec succes "});
       } else {
         console.log("Erreur lors du mise a jour ");
         res.send({message:"Erreur lors du mise a jour "});
       }  

     });

and this is the mongodob document I want to change the field done but with the index from a variable like : var x = 5 " task[x].done = true "

{
"_id" : ObjectId("5a730e55114dbc2a0455c630"),
"email" : "[email protected]",
"password" : "unknown",
"tasks" : [ 
    {
        "title" : "new 5",
        "description" : "dod ododoododododo",
        "date" : "2018-02-07T18:25:14.881Z",
        "done" : false
    }, 
    {
        "title" : "new2",
        "description" : "dod ododoododododo",
        "date" : "2018-02-07T18:25:14.881Z",
        "done" : false
    }
]
}
9
  • What errors do you get with the current code? Commented Feb 11, 2018 at 12:56
  • You u can pass json you can not pass variable Commented Feb 11, 2018 at 13:02
  • Unexpected token + ///___ Can't made a concatination in the $set Commented Feb 11, 2018 at 13:02
  • How can I pass a json instead of var ? Commented Feb 11, 2018 at 13:03
  • let q = { 'tasks.' + req.params.pos + '.done': true} and then $set: q Commented Feb 11, 2018 at 13:05

2 Answers 2

1

From the Mongo docs you need to use the positional $ operator

    db.collection('users')
    .update({'tasks.title': 'new2'}, 
    {'$set': {'tasks.$.done': false}},
    (error,resultat)=>{...
Sign up to request clarification or add additional context in comments.

7 Comments

Still the same problem SyntaxError: Unexpected token +
The problem that if I do like this _____db.collection('users').update( { "_id":ObjectID(req.params.id)}, { $set: { tasks.0.done : true }}_______ it works fine
Look in answers i upload a picture to you
I've updated my answer to use the positional $ operator
This is the correct answer, per the docs. Note that as this answer demonstrates, you must include the array field as part of the query.
|
0

Here is the solution Thank You all for contributing in solving this problem

connection((db)=>{

let tid = { ["tasks."+req.params.pos+".done"] : true };

let query = { "_id": ObjectID(req.params.id) };

db.collection('users').update(
  { "_id":ObjectID(req.params.id)},
  { $set:    tid}
  ,(error,resultat)=>{
       if (resultat){
         console.log("mise a jour avec succes ");
         res.send({message:"mise a jour avec succes "});
       } else {
         console.log("Erreur lors du mise a jour ");
         res.send({message:"Erreur lors du mise a jour "});
       }  

     });
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.