user1337457
user1337457

Reputation: 11

How to update an inner dictionary with a variable name in mongo/meteor?

Specifically, I have a collection called Ideas. An Idea might look like this:

{
    _id:"453uit8ig9", 
    body:"my idea description",
        relations: {
            "9fg8oew74gt9ebh":{weight:1,  unconfirmed:true}, 
            "754787hsdfh":{weight:1,  unconfirmed:true}, 
            "ghtruuy6767":{weight:1,  unconfirmed:true}, 
            "479898ioujhh":{weight:1,  unconfirmed:true}, 
        }
}

The "relations" attribute is a dictionary with IDs of related Ideas as keys, and another dictionary expressing the nature of the relationship as the values.

If I have the ID of an Idea and the ID of one the ideas it is related to, how can I make a mongo update query that will mutate the "unconfirmed" attribute (i.e., set it to false)?

Upvotes: 1

Views: 177

Answers (1)

Dan Dascalescu
Dan Dascalescu

Reputation: 152125

If you rewrite the relations as an array of {relation, weight, unconfirmed}, then you'll be able to use the positional $ operator:

Ideas.update({
  _id: X,
  "relations.relation": Y
}, {
  $set: {
    "relations.$.unconfirmed": false
  }
});

Upvotes: 0

Related Questions