0

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 where I initialize my project:

var someUser = Meteor.users.findOne("W9YEs84QFhZzJeB6j");

var NewIdea = Ideas.insert({
  title: "This Is a Title",
  body: "Blah Blah Blah Body Goes Here",
  userId: someUser._id,
  author: someUser.profile.name,
  timestamp: new Date(now - 5 * 3600 * 1000),
  score: [],
  overallScore: 0,
  votedOnBy: [],
  timesVotedOn: 0
});

Here is what I want it to look like after the update:

{
  _id: "bKXXrpYmppFBfq9Kx",
  title: "This Is a Title",
  body: "Blah Blah Blah Body Goes Here",
  userId: "W9YEs84QFhZzJeB6j",
  author: "Users Name",
  timestamp: ISODate("2016-06-07T20:37:05.775Z"),
  score: [
    {
      userId: ""W9YEs84QFhZzJeB6j",
      score: 1
    }
  ],
  overallScore: 1,
  votedOnBy: [
    "W9YEs84QFhZzJeB6j"
  ],
  timesVotedOn: 1
}

Here is the code I'm using to update (which is not working):

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

Here's the actual document from mongo console (after the update):

{
  "_id" : "bKXXrpYmppFBfq9Kx",
  "title" : "This Is a Title",
  "body" : "Blah Blah Blah Body Goes Here",
  "userId" : "W9YEs84QFhZzJeB6j",
  "author" : "Users Name",
  "timestamp" : ISODate("2016-06-07T20:37:05.775Z"),
  "votedOnBy" : [
    "W9YEs84QFhZzJeB6j"
  ],
  "timesVotedOn" : 1
}

Notice that overallScore and score are not there at all.

I'm new to Meteor and Mongo (perhaps that's obvious). Does anyone know what I'm doing wrong here? Or what I need so that I may do this update right?

2
  • Thanks @Michel Floyd for the code edit :) It's much cleaner now, and you removed other non-necessary information from the database output. Commented Jun 8, 2016 at 3:40
  • I needed to do that to try to figure out your problem. Unfortunately your code looks correct so I'm still missing the solution. Commented Jun 8, 2016 at 3:57

1 Answer 1

2

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
  }
});
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much for your help. I modified my code as you said, but when I run db.ideas.find() from the database, I get an empty score object. Here is a gist with the full details: git.io/voqLK
Since this is getting most of the data into the document, I assume I'll accept this as the answer. And would like to do so. I'm just not sure I'm able to get the object into my document, because I'm not able to verify that it's actually going into my database. Once I figure that bit out, I'll be accepting ur answer. If I need to modify the code to make it do what I need, I'll do that - and then accept your answer. Again, Thanks!
I accepted this answer - since obviously you're right. You solved my first problem. Now I have a second problem which I have opened a new question for: stackoverflow.com/questions/37705040/…
Doh. Nice one David :)
Thanks. I'll admit I had to try out the code before I noticed the problem. It's an easy trap to fall into - surprised more people don't ask this one.
|

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.