mongodb - How can I make a query in mongoose and node.js to count users depending on a value (similar to mysql distinct)? -


i have following collection in mongoose:

_id username problem_no 

and problem_no can either 1, 2 or 3.

i want write webservice in node.js return me json number of records distinguished problem_no, example:

"1" : "17", "2" : "13", "3" : "0" 

i far tried sth like:

 var user            = require('./model.js');   app.get('/countall', function(req, res){       usermodel     // uses mongoose schema run search (empty conditions)     var query = user.count({});     query.exec(function(err, users){         if(err)             res.send(err);          // if no errors found, responds json of users         res.json(users);     }); }); 

but have no idea how introduce here problem_no.. appreciate hints, lot!

i think not possible expect, think have make 3 queries each problem_no:

user.find({problem_no:1})     .count()     .exec(function (err, count) {         res.send({user_problem_no_1: count})     }) 

Comments