MongoDB汇总和MeteorJS中的分组问题 [英] MongoDB Aggregate & Grouping Issue in MeteorJS
问题描述
在Meteor JS中使用MongoDB,如何正确使用Meteor Aggregate?
Using MongoDB in Meteor JS, how do you use Meteor Aggregate properly?
预期结果是返回按用户id分组的用户,并汇总一个名为"progressState"(真/假)的布尔字段.
The intended result is to return grouped users by their userId and sum up a boolean field called "progressState" (true/false).
例如,文档可以具有:
user 001 - true
user 001 - false
user 001 - true
user 003 - false
user 005 - true
但预期结果将是:
user 001: 2 true
user 003: 0 true
user 005: 1 true
etc..
我的尝试出现以下错误:
My attempt gives the following error:
"exception: FieldPath field names may not start with '$'."
这是我的流星代码:
Meteor.publishComposite('completedLB', {
find: function() {
return userCompleted.aggregate([
{
$match: {
"progressState": "true"
}
},
{
$group: {
"_id": "$userId",
"count": {
"$sum": "$progressState"
}
}
},
{
$sort : {
"$progressState": -1
}
}
]);
}
});
推荐答案
如果您使用流星黑客聚合包来实现.aggregate()
命令在您的集合上,那么它只会返回一个数组作为响应.因此,您需要将其处理为已发布的收藏集的形式:
If you are using the meteor hacks aggregate package to implement an .aggregate()
command on your collection, then it will only just return an array in response. So you need to work that into a form of a published collection:
Meteor.publish("completedLB,function() {
var self = this;
var results = userCompleted.aggregate([
{ "$match": { "progressState": true } },
{ "$group": {
"_id": "$userId",
"progressState": { "$first": "$progressState" },
"count": { "$sum": 1 }
}},
{ "$sort": { "_id": 1 } }
]);
_.each(results,function(result) {
self.added("client_collection_name",Random.id(), {
userId: result._id,
progressState: result.progressState,
count: result.count
});
});
self.ready();
});
或者包含false
计数,如建议的输出所示:
Or to include the false
counts as your suggested output suggests itself:
{ "$group": {
"_id": "$userId",
"progressState": { "$first": true },
"count": { "$sum": { "$cond": ["$progressState", 1,0] }
}},
{ "$sort": { "_id": 1 } }
作为带有> $cond
的管道> 评估以转换为数字.
As the pipeline with a $cond
evaluation to convert to numeric.
Where in the basic aggregation you are just "totalling" the matched results and of course the $sort
refers to a field present in the output, which by your example would be the "userId" value now in the _id
key from aggregation, but could also be "count" to order by the total count if wanted.
该部分产生了错误,因为$sort
是当前字段,而不是带有$
表示法的字段值.
That part was producing the error, as $sort
is a present field and not a field value with $
notation.
但是,要发布为客户端可访问的集合,您当然需要将实际的_id
替换为预期的内容.因此,随机id生成在这里可以正常工作,其他字段的包含也是如此.
But of course to publish as a client accessible collection you need to replace the actual _id
with something expected. So random id generation works here, as does the inclusion of the other fields.
有关更多细节,以及仅适用于原始安装的"hacks"软件包的替代,还有这个答案由我自己完成,并以完整列表为例.
For a bit more detail, and also an alternate to the "hacks" package that just works with a vanilla installation, there is also this answerby myself that has a complete listing as an example.
这篇关于MongoDB汇总和MeteorJS中的分组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!