问题
I have a collection following this "schema" :
{
_id: ObjectId,
order: Number,
fieldA: ObjectId,
fieldB: Array[ObjectId]
}
And an index defined like this :
{
fieldA: 1,
fieldB: 1,
order: 1
}
When running a find query like this one :
{
$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 explain plan shows me :
- 4 IXSCANs
- 1 SORT_MERGE
- 1 FETCH
Issue: If I query fieldA
and/or fieldB
with a huge $in
(I'm trying to make something like a join), then the indexing does not act the same, the query is not covered, the sort is made in memory and the explain 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 :/
来源:https://stackoverflow.com/questions/46318304/mongo-in-with-order-indexing