我具有如下所述的文档结构。 我应该将哪些字段标记为索引以优化涉及elemMatch运算符的查询。
db.getCollection('User2').find({results:{$elemMatch:{'address.addressLine1': 'Abc1'}}})
{
"_id" : ObjectId("5cfa26f6b3c275b445d1517b"),
"username" : "123",
"address" : [
{
"addressLine1" : "Abc1",
"addressLine2" : "Pqr1",
"Phone" : "132-123-213",
"pin" : "80001"
},
{
"addressLine1" : "Abc2",
"addressLine2" : "Pqr2",
"Phone" : "132-123-213",
"pin" : "80002"
},
{
"addressLine1" : "Abc3",
"addressLine2" : "Pqr3",
"Phone" : "132-123-213",
"pin" : "80003"
}
]
}
{
"_id" : ObjectId("5cfa2718b3c275b445d15181"),
"username" : "1223",
"address" : [
{
"addressLine1" : "Abc5",
"addressLine2" : "Pqr5",
"Phone" : "132-123-213",
"pin" : "80002"
},
{
"addressLine1" : "Abc2",
"addressLine2" : "Pqr6",
"Phone" : "132-123-213",
"pin" : "80004"
},
{
"addressLine1" : "Abc3",
"addressLine2" : "Pqr7",
"Phone" : "132-123-213",
"pin" : "80005"
}
]
}
我应将“地址”标记为可索引还是“ address.addressLine1”?
我尝试添加复合索引
{
"address.addressLine1" : 1,
"address.pin" : 1
}
,但是查询计划程序不使用它来查询文档。
处理elemMatch运算符时定义索引的最佳实践是什么?