我在 Mongoose 中有以下集合:
_id
username
problem_no
和problem_no
可以是1、2或3。
我想在node.js中编写一个Web服务,它会返回一个json,其中包含许多由problem_no区分的记录,例如:
"1" : "17", "2" : "13", "3" : "0"
到目前为止我尝试过:
var User = require('./model.js');
app.get('/countAll', function(req, res){
userModel
// Uses Mongoose schema to run the search (empty conditions)
var query = User.count({});
query.exec(function(err, users){
if(err)
res.send(err);
// If no errors are found, it responds with a JSON of all users
res.json(users);
});
});
但我不知道如何在这里介绍问题_no.. 我将不胜感激任何提示,非常感谢!
最佳答案
我认为这不可能像您期望的那样,我认为您必须为每个 problem_no
进行 3 次查询:
User.find({problem_no:1})
.count()
.exec(function (err, count) {
res.send({user_problem_no_1: count})
})
关于node.js - 如何在 mongoose 和 node.js 中进行查询以根据值对用户进行计数(类似于 mysql Distinct)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34980151/