Created
February 13, 2015 18:38
-
-
Save cahlan/ac7cdecfcf2c8bed6058 to your computer and use it in GitHub Desktop.
example of queries with a sample controller (node, express, mongoose)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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