> db.d.find()
|
{ "_id" : ObjectId("5ca4fb6f4e1d8532f8828e79"), "a" : 1 }
|
{ "_id" : ObjectId("5ca4fb744e1d8532f8828e7a"), "a" : [ 1, 2 ] }
|
> db.d.aggregate( [ { $group : { _id : "$a" } } ] ) // query without index
|
{ "_id" : 1 }
|
{ "_id" : [ 1, 2 ] }
|
> db.d.createIndex({a:1})
|
{
|
"createdCollectionAutomatically" : false,
|
"numIndexesBefore" : 1,
|
"numIndexesAfter" : 2,
|
"ok" : 1
|
}
|
|
> db.d.aggregate( [ { $group : { _id : "$a" } } ] ) // query with index
|
{ "_id" : 1 } // Different from above!
|
{ "_id" : 2 }
|
It looks like this is because when an index is present, a DISTINCT_SCAN is used. It seems incorrect to use a DISTINCT_SCAN for a $group when the index is multikey (though we should think about this harder).
|