Range query for string
Sample document:
{ "_id": { "$oid": "60a64442cf20f396c3654fb7" }, "level1_1": { "level2_1": [ { "level3_1": 1, "level3_2": "identifier1", "level3_3": [ "ABCCCC", "XYZ024" ] } ] } }
I am trying to make use of atlas search for range querying over strings (See field level1_1.level2_1.level3_3). Consider an example: give me all records where value is “ABC000” to “ABZZZZ”.
From my understanding, there is no operator to support range query for string.
I have tried creating regex query for the above use case. (Assume each of the character can have [0-9A-Z] as possible values)
So the regex may look like: AB[C-Z][0-9C-Z][0-9C-Z][0-9C-Z].
Sample Query:
collection.aggregate( [ { "$search": { "regex": { "query": "AB[C-Z][0-9C-Z][0-9C-Z][0-9C-Z]", "path": "level1_1.level2_1.level3_3", "allowAnalyzedField": True } } }, { "$limit": 1000 }, { "$project": { "_id": 0, "level1_1": 1, "score": { "$meta": "searchScore" } } } ] )
It works but I am not sure how it would perform.
Range query on array of Numbers
Alternately, I have another way to represent the values as integers as well. So I tried doing that.
But still MongoDB atlas has a limitation that it does not support range query on array of number. See https://docs.atlas.mongodb.com/reference/atlas-search/range/#limitation
Sample document schema:
{ "_id": { "$oid": "70a64442cf20f396c3654fb7" }, "level1_1": { "level2_1": [ { "level3_1": 1, "level3_2": "identifier1", "level3_3": [ 50, 100 ] } ] } }
Sample Query:
collection.aggregate( [ { "$search": { "range": { "path": "level1_1.level2_1.level3_3", "gte": 0, "lte": 150 } } }, { "$limit": 1000 }, { "$project": { "_id": 0, "level1_1": 1, "score": { "$meta": "searchScore" } } } ] )
Is there any workaround to perform range query for above use case?
Also, if I were to restructure the schema, any suggested best practices?