How to optimize MongoDB query with both $gt and $lte?

后端 未结 4 1889
后悔当初
后悔当初 2021-02-07 23:53

I have the following query that is kind of like a reverse range lookup:

db.ip_ranges.find({ $and: [{ start_ip_num: { $lte: 1204135028 } }, { end_ip_num: { $gt: 1         


        
4条回答
  •  遥遥无期
    2021-02-08 00:11

    After a ton of experimenting and researching, I came across this:

    https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/IUwOzWsc0Sg

    I'm able to get the query down around 200-300ms with this query, AND dropping all indexes (You have to drop all the indexes for this to work!!!):

    db.ip_ranges.find({start_ip_num: {$lte: 1204135028}, end_ip_num: {$gt: 1204135028}}).limit(1)

    Don't ask me why. I can't explain it. If you're interested, I was building the GeoIP database from MaxMind with MongoDB.

提交回复
热议问题