索引:
{
last_logged_in: -1, // in milliseconds
age: 1,
city: 1,
}
查询:
user.aggregate([
{
$match: {
age: { $gt: 18 },
city: { $in: ["chicago", "paris"] }
}
},
{
$sort: {
last_logged_in: -1
}
},
{
$limit: 10000
}
])
last_logged_in
字段以毫秒为单位。因此,我们进一步假设所有文档都拥有该字段的唯一值。
这个索引在
$match
阶段查找文档有用吗?