Skip to content Skip to sidebar Skip to footer

How To Update An Array Of Subdocuments On A MongoDB Collection In MeteorJS

I'm having a problem updating a mongo collection in Meteor. Perhaps I just don't understand how $addtoset works. Here is my basic document, more or less this is in my fixtures.js w

Solution 1:

It's important to remember that the modifier is just an object. The following object literal:

{
  a: 1, b: 1,
  a: 2, b: 2
}

evaluates to:

{ a: 2, b: 2 }

because the keys are assigned twice, and the last write wins.

In your code, the same idea applies to the $addToSet and $inc keys. To fix it, write your update like this:

Ideas.update("bKXXrpYmppFBfq9Kx", {
  $addToSet: {
    score: { userId: someUser._id, score: 1 },
    votedOnBy: someUser._id
  },
  $inc: {
    overallScore: 1,
    timesVotedOn: 1
  }
});

Post a Comment for "How To Update An Array Of Subdocuments On A MongoDB Collection In MeteorJS"