I have such structure of documents in my products
collection:
{
title: 'Samsung Galaxy S III',
category_id: ObjectId("50bcc2f0b910a6c1936a4424"),
properties: [
{
title: 'OS',
value: 'Android'
},
{
title: 'Display',
value: '4.8"'
}
]
}
Also I've created multikey index: {category_id: 1, 'properties.value': 1, 'properties.title': 1}
What happens when I do query with $all? How it works? Example:
{
category_id: ObjectId("50bcc2f0b910a6c1936a4424"),
properties: {
$all: [
{
$elemMatch: {
title: 'OS',
value: 'Android'
}
},
{
$elemMatch: {
title: 'Display',
value: '4.8"'
}
}
]
}
}
Does it find elements in index by first $elemMatch and then iterate over all found element to ensure second $elemMatch?
For my test collection with 2 androids and 1 ios explain()
returns:
{
"cursor" : "BtreeCursor category_id_1_properties.title_1_properties.value_1",
"isMultiKey" : true,
"n" : 1,
"nscannedObjects" : 2,
"nscanned" : 2,
"nscannedObjectsAllPlans" : 2,
"nscannedAllPlans" : 2,
"scanAndOrder" : false,
"indexOnly" : false,
"indexBounds" : {
"category_id" : [
[
ObjectId("50bcc2f0b910a6c1936a4424"),
ObjectId("50bcc2f0b910a6c1936a4424")
]
],
"properties.title" : [
[
"OS",
"OS"
]
],
"properties.value" : [
[
"Android",
"Android"
]
]
}
}