Skip to content

Instantly share code, notes, and snippets.

@JeremyIglehart
Last active June 8, 2016 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeremyIglehart/0c66d2f401219618a1adf448ca14a0b5 to your computer and use it in GitHub Desktop.
Save JeremyIglehart/0c66d2f401219618a1adf448ca14a0b5 to your computer and use it in GitHub Desktop.
How to Update An Array of Subdocuments on a MongoDB Collection in MeteorJS

How to Update An Array of Subdocuments on a MongoDB Collection in MeteorJS

This is a gist created for a comment to an answer provided on stackoverflow.com.

@David Weldon: Thanks for your answer. Below I edited my input, but got the below output. Maybe this is the default behavior of db.ideas.find(). I'll do some looking around. I expected to also get the subdocuments also when I performed the db.ideas.find().

Code I ran:

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

Output from mongo db console:

meteor:PRIMARY> db.ideas.find()
{
	"_id" : "bKXXrpYmppFBfq9Kx",
	"title" : "Jump through the portal",
	"body" : "The elves from Eldernland should jump through the blue portal mentioned in episode one.",
	"userId" : "W9YEs84QFhZzJeB6j",
	"author" : "Nate Beck",
	"episodeId" : "LbDynnAHxAgM5PPXM",
	"timestamp" : ISODate("2016-06-07T20:37:05.775Z"),
	"votedOnBy" : [
		"W9YEs84QFhZzJeB6j"
	],
	"timesVotedOn" : 3,
	"score" : [
		{
			
		}
	],
	"overallScore" : 1
}

I was expecting to see what was in "Score" this looks like an empty object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment