Reputation: 13
I have seen source code as followings from an online doc.
router.get('/userlist', function(req, res) {
var db = req.db;
var collection = db.get('usercollection');
collection.find({},{},function(e,docs){
res.render('userlist', {
"userlist" : docs
});
});
});
Now I want to try query by user name, i.e. to change collection.find({},{},function(e,docs)
into collection.find({'username':'xxx'},{},function(e,docs)
What I don't understand is collection.find()({},{},function(e,docs){}
what is {} {} means, I tried to change it to collection.find()({'username':'xxx'},{},function(e,docs)
but the result does not change, it still show query results for all users. And what is e and docs parameters and where their values come from?
Upvotes: 0
Views: 39
Reputation: 5848
db.collection.find(<criteria>, <projection>, function(err, docs) {
});
The first argument is the criteria (Specifies selection criteria using query operators. To return all documents in a collection, omit this parameter or pass an empty document).
The second argument is the projection (Specifies the fields to return using projection operators. To return all fields in the matching document, omit this parameter).
The function is a callback where the first argument is an error, and the second is the resulting documents from the query.
More information can be found in the docs. http://docs.mongodb.org/manual/reference/method/db.collection.find/
Upvotes: 1