Skip to content

Instantly share code, notes, and snippets.

@stephentcannon
Created August 19, 2012 18:44
Show Gist options
  • Save stephentcannon/3396983 to your computer and use it in GitHub Desktop.
Save stephentcannon/3396983 to your computer and use it in GitHub Desktop.
Meteor controller, action dynamic query
//this is used on a toggleChild function to add or remove mongo child relationships
//it works fine when for $pull but $addToSet doesn't ever seem to get sent to the server
if(window[Meteor.request.controller.capitalise()]["find"](query).count() == 0){
children[child_collection_name] = child;
modifiers['$addToSet'] = children;
modifiers['$set'] = { client_updated: ts };
} else if (window[Meteor.request.controller.capitalise()]["find"](query).count() == 1){
children[child_collection_name] = child;
modifiers['$pull'] = children;
modifiers['$set'] = { client_updated: ts };
} else {
throw new Meteor.Error(600, 'Invalid toggleChild child count');
}
window[Meteor.request.controller.capitalise()][Meteor.request.action](query, modifiers, function(error){});
// console dumps for pull
// query: {"_id":"ccde90cb-fe57-417e-85ab-cfff80247c3e"}
// modifier: {"$pull":{"users":"6ce98c03-2fa9-48c4-8eb7-8000d104d091"},"$set"{"client_updated":"2012-08-19T18:40:31.487Z"}}
// console dumps for addToSet
// query: {"_id":"ccde90cb-fe57-417e-85ab-cfff80247c3e"}
// modifier: {"$addToSet":{"users":"753634b3-b4ac-4049-808c-052cc42d7ee3"},"$set":{"client_updated":"2012-08-19T18:43:14.534Z"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment