I am trying to query on MongoDB with a compound text index as below:
index : {name : 'text' , createdAt:'-1'}
That will create a compound index for the name and createdAt field, but when I try to query on those fields, it is using in-memory sorting although i defined the sorting order in a compound index.
db.table.find({$text:{$search:"text"}}).sort({createdAt : -1})
I checked MongoDB documentation, but I did not find any use case that matched with mine. I am expecting to remove in-memory sorting with text search with the above compound index.
2
Answers
In MongoDB, when you use a compound index that includes a text index field along with another field, the sort order specified in the compound index is not directly used for sorting when performing a text search. Text search queries have their own internal sorting requirements, and MongoDB may resort to in-memory sorting if the query conditions necessitate it.
In your case, you have a compound text index on { name: ‘text’, createdAt: -1 }. However, when you perform a text search query using $text and sort by createdAt, MongoDB might resort to in-memory sorting if the text search score or other conditions are affecting the sort order.
To optimize queries and reduce the likelihood of in-memory sorting, you can consider the following:
Instead of a compound index, you could create separate indexes for the text search and the sorting field. For example:
Or you can use the projection parameter to limit the fields returned to only those needed. This might help in certain cases:
From the MongoDB documentation (https://www.mongodb.com/docs/manual/core/indexes/index-types/index-text):
Checking the
explain(true)
reveals that only thetext
component of the index is used. Trying to be clever by switching the order of the compound index to:and issuing:
yields this error because a
createdAt
equality is required, not a range operator ($gt
).