I have an ‘advanced search’ on one of my projects that allows users to filter by different fields. I would like to add a ‘length’ filter, for which I can use $strLenCP within my query.
My collections are already pretty well indexed so that the query examines as little documents as possible, however I’m wondering if it’s possible for me to add $strLenCP to an index to make the new query as efficient as possible.
My only idea so far is that I add another field to my collection called ‘length’, loop through the collection to populate it and then index this new field.
Is there a way I can do this without the need to add and populate the extra field?
Thanks in advance.
Welcome back to the MongoDB Community
Can you please clarify if you are referring to the Atlas Search?
Could you please elaborate more on what you mean by “query examines a minimal number of documents”?
Based on my knowledge, it is not possible to use an aggregation operator on the index. If my understanding is incorrect, could you please provide clarification by including some example documents?
What I can conclude from the above statement is that you want to add a new field to the existing document structure and index it. While this is a valid approach, it is generally advisable to index fields that are frequently used in queries. To learn more on this topic, please refer to the following resource to learn more:
However, In order to understand the question better can you can share the following information in addition to the above-asked clarification:
- The MongoDB Version you are using.
- A sample of the document in question.
- The indexes that are present within the collection.
- What is the expected output.
- The query that you have experimented with to achieve the desired results.