Mongodb 查询以查找与特定范围匹配的文档

问题描述 投票:0回答:1

我遇到了两个地理位置之间的距离问题。我有一个 MongoDB 集合,其中包含具有 min_distance、max_distance 和 price 的文档。我需要一个查询来获取我计算的距离的价格。

node.js mongodb mongoose mongodb-query nosql
1个回答
2
投票

试试这个:

distance = 15    
db.collection.find({
   min_distance: { $lt: distance },
   max_distance: { $gt: distance }
})

或者你可以使用表达式:

db.collection.find({
   $expr: {
      $and: [
         { $gt: ["$min_distance", distance] },
         { $lt: ["$max_distance", distance] }
      ]
   }
})

Mongo 游乐场

© www.soinside.com 2019 - 2024. All rights reserved.