Reputation: 639
I want to use a Between-like query in MongoDB for retrieval of the data that includes 24 on 'from' and 'to' fields.
eg, SELECT * FROM Table Where 24 between from and to.
How can I achieve the above query in MongoDB?
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "from" : 10, "to" : 20, "val" : 12 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "from" : 21, "to" : 30, "val" : 21 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b257"), "from" : 31, "to" : 40, "val" : 43 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b258"), "from" : 41, "to" : 50, "val" : 55 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "from" : 51, "to" : 60, "val" : 12 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "from" : 61, "to" : 70, "val" : 9 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "from" : 71, "to" : 80, "val" : 26 }
Desired result:
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "from" : 21, "to" : 30, "val" : 21 }
Upvotes: 0
Views: 645
Reputation: 4034
You can use the $lte and $gte operators together to specify a range.
db.collection.find({ from: { $lte: 24 }, to: { $gte: 24 }})
Upvotes: 3