[英]MongoDB: query greater than value of x by a specfic value
I have a MongoDB database with documents representing locations on a map, the document structure is below.我有一个 MongoDB 数据库,其中包含表示地图上位置的文档,文档结构如下。
I'am trying to query for documents with sw_x
and sw_y
value that is 1000 more or 1000 less than the value of the user location which i get from another post request.我正在尝试查询sw_x
和sw_y
值比我从另一个帖子请求获得的用户位置值多 1000 或 1000 的文档。
This is my get request:这是我的获取请求:
router.get('/getdata', (req, res) =>{
mongoose.connect(url, function(err, db) {
if (err) throw err;
db.collection("mArGo").find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
})
})
Currently this returns all documents in the database, i just need to figure out how to filter them.目前这将返回数据库中的所有文档,我只需要弄清楚如何过滤它们。
So in other words i need my query to return docs with sw_x, sw_y values that are greater than or less than the user location value by 1000所以换句话说,我需要我的查询返回大于或小于用户位置值 1000 的 sw_x、sw_y 值的文档
DB document example:数据库文档示例:
{
_id: new ObjectId("6172a1dcb5ce25759cd9506b"),
epsg: 25832,
filename: 'dom_657000_5354000_500.sfb',
offset_x: -650000,
offset_y: -5360000,
size_x: 500,
size_y: 500,
x_sw: 657000,
y_sw: 5354000
}
In the code, you can calculate min, max of x_sw and y_sw and pass it to query:在代码中,您可以计算 x_sw 和 y_sw 的最小值、最大值并将其传递给查询:
db.collection.find({
x_sw: { $gt: 656000, $lt: 658000 },
y_sw: { $gt: 5353000, $lt: 5355000 }
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.