【问题标题】:How to use indexes in aggregate如何使用聚合索引
【发布时间】:2016-01-28 11:16:05
【问题描述】:

我已经使用 createIndex 创建了索引,然后确保使用 ensureIndex

db.getCollection('artists').createIndex( { featured : NumberInt(-1), physicalCount : NumberInt(-1),digitalCount : NumberInt(-1),createdAt: NumberInt(-1) } )

db.getCollection('artists').ensureIndex( { featured : NumberInt(-1), physicalCount : NumberInt(-1),digitalCount : NumberInt(-1),createdAt: NumberInt(-1) } )

创建索引:

"8" : {
    "v" : 1,
    "key" : {
        "featured" : -1,
        "physicalCount" : -1,
        "digitalCount" : -1,
        "createdAt" : -1
    },
    "name" : "featured_-1_physicalCount_-1_digitalCount_-1_createdAt_-1",
    "ns" : "global-rockstar.artists"
}

现在我想在聚合查询中使用这个索引。我知道当我们在聚合中使用 $project 或 $group 属性时索引不起作用。我想重新设计这个查询,以便它可以使用索引但无法创建一个

db.getCollection('artists').aggregate([{
                $match: query <- initially it is {}
            }, {
                $project: {
                    _id: 1,
                    name: 1,
                    genres_music: 1,
                    slug: 1,
                    country: 1,
                    featured: 1,
                    picture: 1,
                    fans: 1,
                    fans_a: 1,
                    credits: 1,
                    totalPlays: 1,
                    createdAt: 1,
                    fanCount: 1,
                    physicalCount: 1,
                    digitalCount: 1,
                    matches: {
                        $add: [{
                            $cond: [{
                                $or: [{
                                    $eq: ['$featured', true]
                                }, {
                                    $eq: ['$featured', 'on']
                                }]
                            },
                                3, 0
                            ]
                        }, {
                            $size: {
                                $setIntersection: ['$genres_music', usergenres]
                            }
                        }]
                    }
                }
            }, {
                $sort: {
                    matches: -1,
                    physicalCount : -1,
                    digitalCount : -1,
                    createdAt: -1
                }
            }, {
                $skip: pageSize * page  <---- pageSize- 15 , page= 0
            }, {
                $limit: parseFloat(pageSize)
            }])

当我像

这样触发查询时
db.getCollection('artists').find({}).sort({
        //"featured" : -1,
        "physicalCount" : -1,
        "digitalCount" : -1,
        "createdAt" : -1
    })

这是出乎意料的结果

如果有人知道如何使用带有索引的聚合查询,请帮帮我

【问题讨论】:

    标签: node.js mongodb mongoose aggregation-framework


    【解决方案1】:

    根据 MongoDB 文档:

    $match 和 $sort 管道运算符可以利用索引 当它们发生在管道的开始时。

    因此,如果可以的话,您应该将 $sort 阶段放在 $project 和 $group 阶段之前。

    在开始时放置一个 $match 管道阶段,然后是一个 $sort 阶段 管道在逻辑上等价于带有排序的单个查询 并且可以使用索引。如果可能,将 $match 运算符放在 管道的开始。

    此外,最好尽早放置$skip 和$limit 阶段。

    早期过滤 如果您的聚合操作只需要一个子集 集合中的数据,使用 $match、$limit 和 $skip 阶段 限制在管道开头输入的文件。 当放置在管道的开头时,$match 操作使用 合适的索引来仅扫描集合中的匹配文档。

    最后,

    对于复合索引,MongoDB 可以使用索引来支持查询 索引前缀。

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2020-01-02
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 2014-05-11
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多