简体   繁体   English

多个mongoose count()查询到MongoDB

[英]Multiple mongoose count() queries to a MongoDB

first: me -> MongoNoob and I know this has already been asked in one or the other way, but I haven't found anything specific until now. 第一:我 - > MongoNoob,我知道这已经有过一种或另一种方式的问题,但到目前为止我还没有找到任何具体内容。 Let's say I have two Moongoose Models described like this: 假设我有两个这样描述的Moongoose模型:

var pollSchema = mongoose.Schema({
    title: String,
    choices: [{
        content: String
    }]
});
var choiceSchema = mongoose.Schema({
    poll_id: mongoose.Schema.ObjectId,
    option: Number
});

A UI shows the poll and when a user selects a choice, it is written into the choiceSchema model. UI显示轮询,当用户选择一个选项时,它将写入choiceSchema模型。 Now I would like to create a 'statistic', telling me how many users selected option 1, option 2, option 3,.... I could simply fetch all choices for a poll with find and generate the statistic in server code, but if I had a million user choices, I would have to deal with an array of the same size. 现在我想创建一个'统计',告诉我有多少用户选择了选项1,选项2,选项3,....我可以简单地获取find所有选项,并在服务器代码中生成统计信息,但是如果我有一百万用户选择,我将不得不处理相同大小的数组。 This cannot be right. 这不可能是对的。 I could however generate a query and use the count() method: 然而,我可以生成一个查询并使用count()方法:

var query = Choice.find({poll_id: someId}),
query.where('option', 1);
var resultForOption1;
query.count(function(err, count)) {
    resultForOption1 = count;
});

How would I do this for multiple options and 'join' (haha) the results into an array? 我如何为多个选项执行此操作并将结果“加入”(哈哈)到数组中? Since this is all asynchronous I would have nest the calls, but that is not an option for a variable number of queries. 由于这是异步的,我会嵌套调用,但这不是可变数量的查询的选项。

Do I miss the wood for the trees :-)? 我想念树木:-)? Can somebody point me in the right direction? 有人能指出我正确的方向吗?

BR, Daniel BR,丹尼尔

You can use aggregate to do this: 您可以使用aggregate来执行此操作:

Choice.aggregate(
    {$group: {_id: {poll_id: '$poll_id', option: '$option'}, count: {$sum: 1}}}
).exec(...);

This will group the choices collection docs by poll_id and option and count the occurrences of each, giving output that looks like: 这将按poll_idoptionchoices集合文档进行poll_id ,并计算每个文档的出现次数,从而提供如下输出:

{
  "result": [
    {
      "_id": {
        "poll_id": 2,
        "option": 3
      },
      "count": 1
    },
    {
      "_id": {
        "poll_id": 1,
        "option": 2
      },
      "count": 2
    },
    {
      "_id": {
        "poll_id": 2,
        "option": 2
      },
      "count": 1
    },
    {
      "_id": {
        "poll_id": 1,
        "option": 1
      },
      "count": 1
    }
  ],
  "ok": 1
}

You can then use subsequent $group and $project stages in your aggregate pipeline to further group and reshape the generated docs as needed. 然后,您可以在aggregate管道中使用后续$group$project阶段,以根据需要进一步对生成的文档进行分组和重新整形。

If you want just iterate over the count queries and do it easy and clean you can use a lib like co or even better, async: 如果你想迭代count查询并轻松清理你可以使用像co这样的lib甚至更好,async:

async.map({ "option":1 },{ "option":2 }], Choice.find, function(e, r){
    // And you continue from here
});

Another option -better in my opinion-. 另一个选择 - 在我看来更好 - 。 is to use aggregate queries . 是使用聚合查询 You will learn more about mongoDB and should be more efficient. 您将了解有关mongoDB的更多信息,并且应该更有效率。 In the link I send to you the first example is almost what you want to do: 在我发给你的链接中,第一个例子几乎就是你想要做的:

询问
(source: mongodb.org ) (来源: mongodb.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM