Jeremy M.
Jeremy M.

Reputation: 1164

Mongoose - Check for a value between two numbers

i have a mongoose document that contains two ages. The minimum age to go to the attraction and the maximum one.

I need to query my database to get all attractions between 0 and 2 years old.

But this won't work :

"ageMin": {
  "$lte": 2
},
"ageMax": {
  "$gte": 0
},

Because most of my attraction are between 0 and 99.

To help you visualise i made an example:

attractionOne: {
  name: "Deadly Looping",
  ageMin: 12,
  ageMax: 99
}
attractionTwo: {
  name: "Easy Ladybug",
  ageMin: 0,
  ageMax: 12
}

Thank you.

Upvotes: 2

Views: 5274

Answers (1)

mickl
mickl

Reputation: 49975

If you need to match if those ranges are overlapping with 0-2 range then you're close, just need to check if 0-2 range is between min and max age. Try:

db.attractions.find({ ageMin: { $lte: 0 }, ageMax: { $gte: 2 } })

Upvotes: 5

Related Questions