Skip to content

Instantly share code, notes, and snippets.

@cahlan
Created February 13, 2015 18:38
Show Gist options
  • Save cahlan/ac7cdecfcf2c8bed6058 to your computer and use it in GitHub Desktop.
Save cahlan/ac7cdecfcf2c8bed6058 to your computer and use it in GitHub Desktop.
example of queries with a sample controller (node, express, mongoose)
module.exports = {
getCustomer: function(req, res) {
///api/customers/:id
Customer.findOne({_id: req.params.id}).exec().then(function(err, user) {
return res.json(user);
});
},
getCustomers: function(req, res) {
var sort = req.query.sort || '-createdAt';
var skip = req.query.skip || 0;
var limit = Number(req.query.limit) || 100;
if (limit > 1000) {
limit = 1000;
}
///api/customers
Customer
.find()
.limit(limit) //optional
.skip(skip) //optional
.sort(sort) //optional
.select('name email address') //optional
.exec().then(function(err, users) {
return res.json(users);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment