Skip to content

Instantly share code, notes, and snippets.

@cmalven
Created December 8, 2012 16:39
Show Gist options
  • Save cmalven/4240928 to your computer and use it in GitHub Desktop.
Save cmalven/4240928 to your computer and use it in GitHub Desktop.
Meteor Mongo Updating
// Replaces all of Vote.users array
Votes.update(
{_id: '32dc6fa5-052e-4e2c-8a90-4bd235bf3b0d'},
{
$set: {
'users': [
{
id: '2f537a74-3ce0-47b3-80fc-97a4189b2c15',
vote: 0
},
{
id: '6d46c5a6-d014-4ef8-b78b-c4638fc2cd7f',
vote: null
},
{
id: '8bffafa7-8736-4c4b-968e-82900b82c266',
vote: 1
},
{
id: 'a359da7f-a0d1-4f41-96e6-449dcde09a33',
vote: null
}
]
}
}
)
// Set an individual users vote (by their index)
Votes.update({}, {$set: {'users.1.vote': 2}})
// Reset vote in all of vote.users array
vote = Votes.findOne({_id: vote_id})
_.each vote.users, (user) ->
Votes.update(
{_id: vote_id, 'users.id': user.id},
{$set: {'users.$.vote': null}}
)
// Remove a particular field
Votes.update({_id: '32dc6fa5-052e-4e2c-8a90-4bd235bf3b0d'}, {$unset: {user: 1}})
// Adds to vote.options array
Votes.update(
{_id: '32dc6fa5-052e-4e2c-8a90-4bd235bf3b0d'},
{
$push: {
options: {
option: 'Whatever'
}
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment