我想使用地理空间 geoNear,mongodb 中的数据库,Mongo 查询:
db.runCommand(
{
geoNear: "tmp",
near: { type: "Point", coordinates: [ 77.00000, 12.00000] },
spherical: true,
maxDistance : 200
}
)
在 mongo 终端中给出结果,但如何使用 node.js 执行它我正在使用 mongo-pool 和 generic-pool 进行 mongo 池化?
最佳答案
使用 $geoNear
聚合管道的运算符(如果您使用的是 MongoDB 2.4 或更高版本,则可用)。例如:
var mongodb = require('mongodb')
, MongoClient = mongodb.MongoClient
, express = require('express')
, app = express();
var db;
MongoClient.connect(process.env.MONGOHQ_URL, function(err, database) {
db = database;
app.listen(1337);
});
app.get('/geospatial', function(req, res) {
db.collection('collection_name').aggregate([
{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [parseFloat(req.params.lng), parseFloat(req.params.lat)]
},
"distanceField": "distance",
"maxDistance": 200,
"spherical": true,
"query": { "loc.type": "Point" }
}
},
{
"$sort": {"distance": -1} // Sort the nearest first
}
],
function(err, docs) {
res.json(docs);
});
});
关于node.js - 如何在 nodejs 中使用 geoNear?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29255525/