【发布时间】:2016-06-02 05:50:11
【问题描述】:
我正在使用 mongodb 作为 Android 应用程序的数据库构建 nodejs api。 当 Android 用户将他的 GPS 位置发送到后端时,API 会根据与用户的距离对所有数据进行排序并回复。
为此,我在聚合框架中使用 $geoNear 阶段。我按照说明进行操作,但无法获取数据,只有“未定义”。
这是来自 db 的 JSON 数据格式。
{
userId: "",
description: "",
location: {
type: "Point",
coordinates: [ latitude, longitude ]
},
image: ""
}
这是我的 geoNear 代码。
db.posts.createIndex({location:"2dsphere"});
db.posts.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ parseFloat(geoInfo.latitude) , parseFloat(geoInfo.longitude) ] },
distanceField: "distance",
spherical: true
}
},
{ $sort: { distance: 1 } },
{ $limit: 20 }
], function(err, docs) {
if (err) {
callback(err, null);
}
callback(null, docs);
});
我只能看到未定义的结果。 现在我在这个问题上停留了几天。 非常感谢任何帮助!
【问题讨论】: