首页 文章

Postman(和python eve)在mongodb中完成时为同一查询提供不同的结果

提问于
浏览
1

我正在使用python eve开发一个非常轻量级的API,它访问一个mongodb数据库 . 数据库中的每个文档都有一个geom字段,该字段上有一个2d球体索引 .

当我在mongo中运行此查询时,它可以非常快速地运行

db.api.aggregate({"$geoNear": {"near": {"type": "Point", "coordinates": [-1.11, 51.69]}, "distanceField": "distance", "maxDistance": 100, "num": 2, "spherical": "true"}}).pretty()

但是当我在邮递员中运行它时,它只返回所有内容并忽略查询

http://localhost:8090/data?aggregate={"$geoNear": {"near": {"type": "Point", "coordinates":  [-1.11, 51.69]}, "distanceField": "distance", "maxDistance": 100,"num": 2,"spherical": "true"}}

我在Eve中设置了一个基本架构,部分工作 . 它只返回_id而不返回作为查询的一部分创建的距离字段 . 虽然我运行的假设一旦我有邮递员的语法正确,这将工作 .

api_shema = {'_id': {'type': 'string'},
                      'distance': {'type': 'string'}
                      }

我也设置了这个项目

line_info_item = {'item_title': 'line_info',
                        'resource_methods': ['GET'],
                        'schema': api_shema,
                        'datasource': {
                            'source': 'api',
                            'filter': {'_type': 'line'}
                            }
                     }

最后添加以下域名

DOMAIN = {'line_info': line_info_item}

任何有关邮递员查询的帮助,或者如果您发现其他任何错误,将非常感激 .

编辑:

我根据Neil的答案在 endpoints 上设置了管道,但它仍然忽略了查询并返回所有 .

DOMAIN = {'line_info': line_info_item,
            'aggregation': {
                'pipeline': [{
                    "$geoNear": {
                        "near": {
                            "type": "Point", 
                            "coordinates": ["$coords"]
                        },
                        "distanceField": "distance", 
                        "maxDistance": "$maxDist",
                        "num": 10,
                        "spherical": "true"
                    }
                }]
            }
        }

邮递员查询网址是

http://localhost:8090/data?aggregate={"$maxDist":500, "$coords":[-1.477307, 50.931700]}

编辑

虽然忽略了架构,但是工作有点......但是猜测这是一个不同的问题 .

将聚合管道移动到项目中并删除“$ coords”周围的方括号

river_relate_item = {'item_title': 'line_info_item',

                        'resource_methods': ['GET'],

                        'schema': api_shema,

                        'datasource': {
                            'source': 'api',
                            'filter': {'_type': 'line'},
                            'aggregation': {'pipeline': [{'$geoNear':{'near':{'type': 'point', 'coordinates': '$coords'},'distanceField': 'distance','maxDistance': '$maxDist','num': 1, 'spherical': 'true'}}]}
                            },

                    }

1 回答

  • 0

    虽然忽略了架构,但是工作有点......但是猜测这是一个不同的问题 .

    将聚合管道移动到项目中并删除“$ coords”周围的方括号

    river_relate_item = {'item_title': 'line_info_item',
    
                            'resource_methods': ['GET'],
    
                            'schema': api_shema,
    
                            'datasource': {
                                'source': 'api',
                                'filter': {'_type': 'line'},
                                'aggregation': {'pipeline': [{'$geoNear':{'near':{'type': 'point', 'coordinates': '$coords'},'distanceField': 'distance','maxDistance': '$maxDist','num': 1, 'spherical': 'true'}}]}
                                },
    
                        }
    

相关问题