如何在mongoDB中组合索引

问题描述 投票:0回答:1

我的管道

'pipeline' => [
            ['$match' => ['username' => 'xxx']],
            ['$sort' => ['create_at' => 1]],
            ['$limit' => 1]
        ]

创建索引以匹配或排序?

mongodb
1个回答
0
投票

这是一个shell查询。 hint选项强制使用指定的索引。 https://docs.mongodb.com/manual/reference/command/aggregate/

db.collection.aggregate([
        {'$match' : {'username' : 'xxx'}},
        {'$sort' : {'create_at' => 1}},
        {'$limit' => 1},
        { hint: { username: 1, create_at: 1 } }
) 
© www.soinside.com 2019 - 2024. All rights reserved.