我的产品集合中有 3 个文档:
samsung = {
title: 'Samsung Galaxy S III',
category_id: ObjectId("50bcc2f0b910a6c1936a4424"),
properties: [
{
title: 'OS',
value: 'Android'
},
{
title: 'Display',
value: '4.8"'
}
]
}
htc = {
title: 'HTC One X',
category_id: ObjectId("50bcc2f0b910a6c1936a4424"),
properties: [
{
title: 'OS',
value: 'Android'
},
{
title: 'Display',
value: '4.7"'
}
]
}
apple = {
title: 'Apple iPhone 5',
category_id: ObjectId("50bcc2f0b910a6c1936a4424"),
properties: [
{
title: 'OS',
value: 'iOS'
},
{
title: 'Display',
value: '4"'
}
]
}
和索引{category_id: 1, 'properties.title': 1, 'properties.value': 1}
。
我认为索引应该是这样的:
ObjectId("50bcc2f0b910a6c1936a4424")
OS
Android
samsung
htc
iOS
apple
Display
4.8"
samsung
4.7"
htc
4"
apple
我期望该查询:
{
category_id: ObjectId("50bcc2f0b910a6c1936a4424"),
properties: {
$elemMatch: {
title: 'OS',
value: 'Android'
}
}
}
nscanned == 2,nscannedObjects == 2,n == 2。但我得到这样的输出explain()
:
{
"cursor" : "BtreeCursor category_id_1_properties.title_1_properties.value_1",
"nscanned" : 3,
"nscannedObjects" : 3,
"n" : 2,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : true,
"indexOnly" : false,
"indexBounds" : {
"category_id" : [
[
ObjectId("50bcc2f0b910a6c1936a4424"),
ObjectId("50bcc2f0b910a6c1936a4424")
]
],
"properties.title" : [
[
"OS",
"OS"
]
],
"properties.value" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
}
}
你能解释一下为什么indexBounds
不是"properties.value"
吗["Android","Android"]
?
我可以重写查询或重建索引以使用索引获得正确的手机吗?