0

I have following command:

collection.update(
   { "facebook_id": req.params.facebook_id }, 
   { "$push": { "circles.0.messages": message } }, function(err) {...});

That works fine. But I need the 0 to be a variable. As soon as I change it to:

collection.update(
    {"facebook_id": req.params.facebook_id },
    { "$push": { "circles.j.messages": message } }, function(err) {...});

It doesn't work anymore. There is no error, but nothing is being pushed. Tries like:

  collection.update(
      {"facebook_id':req.params.facebook_id }, 
      { "$push": { "circles."+j+"j.messages": message } }, function(err) {...});

also won't work.

How can I use a variable in Dot Notation?

Thanks & Best, Marc

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Luxori
  • 115
  • 7
  • I think you may be looking for the `$` positional operator? http://docs.mongodb.org/manual/reference/operator/update/positional/ – mnemosyn Apr 29 '14 at 16:41

2 Answers2

0

As @mnemosyn mentioned:

collection.update(
    {"facebook_id": req.params.facebook_id, "circles.field" : value }, 
    { "$push": { "circles.$.messages": message } }, function(err) {...});

works fine!

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Luxori
  • 115
  • 7
0

I find the answers you got rather unsatisfying, this is how it can be done: Hardcoded in shell:

"objectName.nestedObjectname";

With variable:

let myVar = "nestedObjectname"; 
[`objectName.${myVar}`];
S1r_Mar71n
  • 125
  • 5