首页 > 解决方案 > 使用距离过滤 mongodb 文档

问题描述

我很难使用用户的坐标(最大距离)过滤文档。

我的请求如下所示:

app.post('/item', checkAuth, function(request, response) {

    var location = request.body.location;
    var distance = request.body.distance;

    Item.find( {  "location": {
        $near: {   $geometry: {  type: "Point",
                               coordinates: [location.longitude, location.latitude] },
                               $maxDistance: distance
                              
               }
    } }, function(err, items) {
        if (err) {
            response.status(500).send({error: err});
        } else {
            response.send({items : items,
                           message: 'Fetching items successful'
                          });
        }
    });
});

标签: node.jsmongodb

解决方案


推荐阅读