【问题标题】:Mongodb $geoNear operator not working correctlyMongodb $geoNear 运算符无法正常工作
【发布时间】: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);
});

我只能看到未定义的结果。 现在我在这个问题上停留了几天。 非常感谢任何帮助!

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    这是工作代码。

    db.open(function(err, db) {
    var collection = db.collection("posts");
    
    console.log(collection.listIndexes);
    
    // Wait for a second before finishing up, to ensure we have written the item to disk
    setTimeout(function() {
        collection.aggregate([ {
            $geoNear : {
                near : {
                    type : "Point",
                    coordinates : [ parseFloat(-73.97), parseFloat(40.77) ]
                },
                distanceField : "distance",
                spherical : true
            }
        }, {
            $sort : {
                distance : 1
            }
        }, {
            $limit : 20
        } ], function(err, docs) {
            if (err) {
                console.log(err);
            }
            console.log("completed...........");
            console.log("doc ====>" + JSON.stringify(docs));
        });
    }, 1000);
    });
    

    插入和创建索引命令:-

    db.posts.insert(
    {
      userId : "user1",
      description : "desc 1",
      loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
      name: "Central Park",
      category : "Parks",
      image : "image 1"
    }
    )
    
    db.posts.insert(
    {
      userId : "user2",
      description : "desc 2",
      loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
      name: "La Guardia Airport",
      category : "Airport",
      image : "image 2"
    }
    )
    
    db.posts.createIndex( { loc : "2dsphere" } )
    

    【讨论】:

    • 您好 Notionquest,感谢您的友好回答。您的代码似乎可以处理示例数据,但是当我将它带到我的数据库时,它也无法正常工作。
    • 这里有一个错误。 "errmsg": "exception: geoNear command failed: { errmsg: \"exception: 'near' field must be point\", code: 17304, ok: 0.0 }",
    • 请提供您的文件的准确样本副本(1 或 2 份文件)。否则,将很难调试您的问题。
    • 我找到了解决方案。原因是我用 [lat, lng] 写了坐标。实际上它应该由 [lng, lat] 编写。感谢您的帮助
    猜你喜欢
    • 2015-11-27
    • 2015-04-25
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多