我想在将保存数组的字段上创建一个索引。默认情况下,当在这样的字段上创建索引时,mongo 会分别索引每个数组中的每个项目。
但是,我希望索引改为复合索引,类似于子文档字段上的索引(我希望索引是唯一的,并且['a', 'b']
与 不同['b', 'a']
)。有没有办法在mongo中做到这一点?
You can do this by storing the data as follows:
{
field: { v: [ a, "b" ] }
}
And then do an index on:
db.collection.ensureIndex( { field: 1 } );
I know it's a bit counter intuitive, but an index on "field" will now use as index value:
v: [ a, "b" ]
And not each a and "b" individually. Of course you can use something else for "v", but it is important that the value for field is a document, and not an array.
A query on:
db.collection.find( { field: { v: [ 'a', 'b' ] } } )
Will then happily use the index:
{
"cursor" : "BtreeCursor field_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"field" : [
[
{
"v" : [ "a", "b" ]
},
{
"v" : [ "a", "b" ]
}
]
]
},
"server" : "whisky:27017"
}
对于在包装字段上索引时它是否使用索引的问题,是的,它确实使用了索引。以下来自 shell 的语句证明了这一点。
> db.arrayindtest.insert({_id:1, f:{a:["a","b"]}})
> db.arrayindtest.insert({_id:2, f:{a:["b","a"]}})
> db.arrayindtest.insert({_id:3, f:{a:["a","b", "c"]}})
> db.arrayindtest.ensureIndex({f:1})
> db.arrayindtest.find({f:{a:["a","b"]}})
{ "_id" : 1, "f" : { "a" : [ "a", "b" ] } }
> db.arrayindtest.find({f:{a:["a","b"]}}).explain()
{
"cursor" : "BtreeCursor f_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"f" : [
[
{
"a" : [
"a",
"b"
]
},
{
"a" : [
"a",
"b"
]
}
]
]
},
"server" : "sridhar-PC:27017"
}
>