javascript 如何计算猫鼬中具有一个不同字段的记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12451575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count records with one distinct field in mongoose?
提问by askmike
While exploring mongoose for nodejs I ran into the problem of needing to know the amount of user in my collection:
在为 nodejs 探索 mongoose 时,我遇到了需要知道我的集合中的用户数量的问题:
My collection has records, each record has a user. I want to know the amount of unique (different) users.
我的收藏有记录,每条记录都有一个用户。我想知道唯一(不同)用户的数量。
How can I do this with mongoose?
我怎么能用猫鼬做到这一点?
EDIT:
编辑:
The database is growing quite fast, is there anyway to get the number back from the DB instead of getting all the distinct records and counting them?
数据库增长得非常快,无论如何要从数据库中取回数字而不是获取所有不同的记录并对其进行计数?
回答by JohnnyHK
Here's an alternative answer as I get an exception when I try Reddest's approach with Mongoose 3.1.2 (which seems like a bug in Mongoose to me as Reddest's approach should be fine).
这是一个替代答案,因为当我使用 Mongoose 3.1.2 尝试 Reddest 的方法时出现异常(这对我来说似乎是 Mongoose 中的一个错误,因为 Reddest 的方法应该没问题)。
You can call the distinct
method on your collection's model, specifying the name of the user-identifying field of that collection:
您可以调用distinct
集合模型上的方法,指定该集合的用户标识字段的名称:
Record.distinct('user_id').exec(function (err, user_ids) {
console.log('The number of unique users is: %d', user_ids.length);
});
Or if you want to chain the distinct
call from a find, include the callback in the distinct
call (this did work for me):
或者,如果您想distinct
从 find链接调用,请在调用中包含回调distinct
(这对我有用):
Record.find().distinct('user_id', function (err, user_ids) { ... });
UPDATE
更新
If you just want the count without getting the values, stick a count()
call in the chain:
如果您只想要计数而不获取值,count()
请在链中调用:
Record.distinct('user_id').count().exec(function (err, count) {
console.log('The number of unique users is: %d', count);
});
NOTE: this doesn't work in the latest Mongoose code (3.5.2).
注意:这在最新的 Mongoose 代码 (3.5.2) 中不起作用。
回答by aaronheckmann
Aggregation will work for you. Something like that:
聚合对你有用。类似的东西:
Transaction.aggregate(
{ $match: { seller: user, status: 'completed' } },
{ $group: { _id: '$customer', count: {$sum: 1} } }
).exec()
回答by ScorpioCPH
If you just want get the number of queried collections, you can use this:
如果您只想获取查询集合的数量,可以使用以下命令:
Record.find()
.distinct('user_id')
.count(function (err, count) {
//The number of unique users is 'count'
});
回答by BadCanyon
You can do a distinct query.
您可以进行不同的查询。
var Record = db.model('Record', yourSchema);
Record.find().distinct('user').exec(callback);
Mongoose Queries: http://mongoosejs.com/docs/queries.html
猫鼬查询:http: //mongoosejs.com/docs/queries.html
MongoDB distinct query: http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct
MongoDB 不同查询:http: //www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct
回答by Leopold Kristjansson
I just needed the number of distinct musicians, but some of the code above did not work for me. If I used count and distinct together I just got the total number.
我只需要不同音乐家的数量,但上面的一些代码对我不起作用。如果我将 count 和 distinct 一起使用,我只会得到总数。
This was my solution:
这是我的解决方案:
/**
* Get number of distinct musicians
*/
myList.find()
.distinct('musicianName')
.exec(function (err, count) {
console.log(count.length);
});