具有索引的Mongodb聚合性能

时间:2017-10-26 18:45:43

标签: mongodb mongodb-query aggregation-framework

我有一个类似于此结构的集合。

{
        "_id" : ObjectId("59d7cd63dc2c91e740afcdb"),
        "dateJoined": ISODate("2014-12-28T16:37:17.984Z"),
        "dateActivatedMonth": 15, 
        "enrollments" : [
            { "month":-10, "enrolled":'00'},
            { "month":-9, "enrolled":'00'},
            { "month":-8, "enrolled":'01'},
            //other months
            { "month":8, "enrolled":'11'},
            { "month":9, "enrolled":'11'},
            { "month":10, "enrolled":'00'}
        ]
    }

dateActivatedMonth是dateJoined的月数。

月份的注册子文档是dateJoined的相对月份。

我正在使用Mongodb聚合框架来处理类似"所有注册为' 01'并且“登记的月份是激活前10个月和激活后25个月”。 &#34 ;.

在我的聚合中,首先我在$ match管道中应用所有可能的过滤器,并在" month"上应用条件。在$ project管道中。

db.getCollection("enrollments").aggregate(
    { $match:{ //initial conditions }},
    { $project: {
          //Here i will apply month filter by considering the number of month before and after.
       }
    }
    //other pipeline operations
)

我在我的条件下应用的所有过滤器都是可选的。所以在某些情况下$ match不会过滤任何东西。保证存在的唯一条件是"月"在"注册"子文件。

此查询很慢。大约需要6-7秒。 (数据也很庞大)。我正在寻找改进方法。我要看的第一件事是创建索引。

现在我的问题是:

  1. $ project可以使用索引吗?我尝试在月份创建索引"但是我没有在queryPlanner中看到有关索引使用情况的任何内容与explain()

  2. 我想移动"月"条件为$ match,以便它使用索引。如何在$ match中使用字段值?像这样的东西:

    db.getCollection('collection').aggregate([
    {
        $match:{
            dateActivatedMonth: {$exists: true}
            //,'enrollment.month': dateActivatedMonth  -- not working
            //,'enrollment.month': '$dateActivatedMonth' -- not working
        }
    }])
    
  3. 感谢您的耐心等待。

0 个答案:

没有答案