Mongo $ in with order indexing

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: /

0
sorting indexing mongodb in-operator
source share

No one has answered this question yet.

See similar questions:

one
How does sorting work with `$ or` and` $ in` queries in MongoDB?

or similar:

2302
How does database indexing work?
1015
What does a clustered and nonclustered index really mean?
682
How to list all collections in mongo shell?
thirteen
How does index sorting work in MongoDB?
2
Why is MongoDB misusing the index?
0
Mongodb: multi-component aggregate group not using index
0
indexing to use to use closed query
0
Why does MongoDB handle requests differently when they are submitted from the same index?
0
The correct mongo index for a large query
0
MongoDB query covered by index, but $ exists field not contained in index

All Articles