I'm troubleshooting an issue with updating a mongo collection in my Meteor project.
It seems like I might be misunderstanding how $addtoset
is supposed to function.
Below is the basic structure of my document, which is defined in my fixtures.js
during project initialization:
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
});
This is the desired updated version of the document:
{
_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
}
This is the update code that I've attempted (without success):
Ideas.update("bKXXrpYmppFBfq9Kx", {
$addToSet: {score: {userId: someUser._id, score: 1}},
$inc: {overallScore: 1},
$addToSet: {votedOnBy: someUser._id},
$inc: {timesVotedOn: 1}
});
After executing this update, here's the resulting document displayed in the Mongo console:
{
"_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
}
As observed, the fields overallScore
and score
are missing entirely.
Given my limited experience with Meteor and Mongo, I'm reaching out for guidance on what could be amiss with this update process. Any advice or insights are greatly appreciated!