mongoDB
supports indexing keys which values are arrays. These multikey indexes allow
MongoDB to return documents from queries using the value of an array.
> db.sample.find()
{ "_id" : ObjectId("54c8f40c889636817c7be6a3"), "a" : 1, "b" : 2 }
{ "_id" : ObjectId("54c8f451889636817c7be6a4"), "a" : 3, "b" : 4 }
{ "_id" : ObjectId("54c8f699889636817c7be6a5"), "c" : [ 1, 2, 3, 4 ] }
>
> db.sample.ensureIndex({c:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
>
> db.sample.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.sample"
},
{
"v" : 1,
"key" : {
"a" : 1,
"b" : -1
},
"name" : "a_1_b_-1",
"ns" : "test.sample"
},
{
"v" : 1,
"key" : {
"c" : 1
},
"name" : "c_1",
"ns" : "test.sample"
}
]
Observe
below snippet, (isMultiKey : true), it is using multi key index.
> db.sample.find({c:1}).explain()
{
"cursor" : "BtreeCursor c_1",
"isMultiKey" : true,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 1,
"nChunkSkips" : 0,
"millis" : 85,
"indexBounds" : {
"c" : [
[
1,
1
]
]
},
"server" : "RENT-MIS-LT3016:27017",
"filterSet" : false
}
Limitation
MongoDB
don’t allow compound indexes multikey compound indexes, at most one field in a
compound index may hold an array. For example, given an index on { c: 1, d: 1
}, the document {c:[4,5,6], d:[4,5,6,7,8]} is impermissible. If you attempt to
insert such a document, MongoDB will reject the insertion, and produce an error
that says cannot index parallel arrays.
> db.sample.ensureIndex({c:1, d:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1
}
>
> db.sample.insert({c:[4,5,6], d:[4,5,6,7,8]})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 10088,
"errmsg" : "insertDocument :: caused by :: 10088 cannot index parallel arrays [d] [c]"
}
})
>
Prevoius Next Home
No comments:
Post a Comment