Python在使用聚合管道时没有遵循set架构并返回完整文档

2024-04-29 04:31:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的api,其中提供了坐标和距离,并返回了该距离内的一个参数和文档。我希望它只返回id和距离,但是定义的模式被忽略,整个文档被返回。有什么想法吗?你知道吗

item = {'item_title': 'relate',
        'datasource': {
            'source': 'api',
            'filter': {'_type': 'line'},
            'aggregation': {'pipeline': [{'$geoNear':{'near':{'type': 'point', 'coordinates': '$coords'},'distanceField': 'distance','maxDistance': '$maxDist','num': 1, 'spherical': 'true'}}]}
            },
         'schema': {
             '_id': {'type': 'string'},
             'distance': {'type': 'float'}
             },
         } 
DOMAIN = {"data": item}

邮递员的查询是:

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

编辑:

根据尼尔的评论,我试着这样做:

item = {'item_title': 'relate',
             'schema': {
                'uri': {'type': 'string'},
                'distance': {'type': 'float'}
                },
            'datasource': {
                'source': 'api',
                'filter': {'_type': 'link'},
                'aggregation': {'pipeline': [{'$geoNear':{'near':{'type': 'point', 'coordinates': ['$lng', '$lat']},'distanceField': 'distance','maxDistance': '$maxDist','num': 1, 'spherical': 'true'}}]}
                }
            }

随以下邮递员请求:

http://localhost:8090/data?aggregate={"$maxDist": 500, "$lng": -1.47, "$lat": 50.93}

这将导致以下错误:

geoNear command failed: { ok: 0.0, errmsg: "'near' field must be point", code: 17304, codeName: "Location17304" }

Tags: 文档apiid距离datatitletypeitem