Skip to content

Instantly share code, notes, and snippets.

@johnschimmel
Last active December 17, 2015 00:38
Show Gist options
  • Save johnschimmel/5522134 to your computer and use it in GitHub Desktop.
Save johnschimmel/5522134 to your computer and use it in GitHub Desktop.
var friendQuery = models.User.findById(userID);
friendQuery.exec(function(err, currentUser){
// filter Users by _id using .friends id array
// $in operator in Mongo allows you to use an array as search param
// http://docs.mongodb.org/manual/reference/operator/#AdvancedQueries-%24in
// in mongoose http://mongoosejs.com/docs/api.html#query_Query-in
var filter = { _id : { $in : currentUser.friends } };
// select the fields you want
var fields = 'name photo';
// build and run the query,
// you'll get an array of the objects with the fields
// or you'll get an error.
var query = models.User.find(filter, fields);
query.exec(function(err, friendData){
res.json(friendData); // return json
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment