I have a collection following this "schema":
{ _id: ObjectId, order: Number, fieldA: ObjectId, fieldB: Array[ObjectId] }
And an index defined as follows:
{ fieldA: 1, fieldB: 1, order: 1 }
When running a search query like this:
{ $and: [ {fieldA: {$in: [{"$oid":"592edae196232608d00f78f5"},{"$oid":"592edadc96232608d00f5614"}]}}, {fieldB: {$in:[{"$oid":"592edace96232608d00ef77f"},{"$oid":"592edacd96232608d00ef34b"}]}} ] }
with sort defined as
{ order: 1 }
The query runs fine, the index covers the query , and the explanation plan shows me:
- 4 IXSCANs
- 1 SORT_MERGE
- 1 FETCH
Problem. If I ask fieldA and / or fieldB with a huge $in (I'm trying to do something like a join), then the indexing doesn't work the same, the query doesn't cover , the sorting is done in memory , and the explanation plan shows me:
- 1 IXSCAN
- 1 FETCH
- 1 SORT_KEY_GENERATOR
- 1 SORT
Even worse, if I run this query from Mongoose, the sort operation goes out of memory: /
sorting indexing mongodb in-operator
Cyril Chapon
source share