Reputation: 43
I have a model name product which has a key ageGroup: ["10 - 12", "13 - 15"]
array of range strings
I need to query all the elements if i pass the ageGroup 10 or 11 or 15 in number or in string.
For ex
db.product.find({ageGroup: 11})
Or
db.product.find({ageGroup: 15})
Sorry for bad question typing, using mobile app.
Upvotes: 0
Views: 445
Reputation: 5245
If you have predefined list of strings of ranges. You can find the range first from that list, outside MongoDB, then use that string to query. The implementation will depend on the language you are using.
If you don't have that information in advance. You can use $map
, $split
, $toInt
, $arrayToObject
in aggregation pipeline to convert ["10 - 12", "13 - 15"]
to something like [{ "from": 10, "to": 12 }, { "from": 13, "to": 15 }]
which will be easier to filter.
Upvotes: 0
Reputation: 11
If you are saving this in same format then you can use substr aggregated query to extract data. https://docs.mongodb.com/manual/reference/operator/aggregation/substr/
Upvotes: 1