0

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.

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

thanks for your help, using array of objects helped alot. But in my case there was no need to save array of objects in db. I did it anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.