首页 > 解决方案 > MongoDB & Parse Server - 无法基于 geoPoint 进行查询

问题描述

简单查询:

PFQuery *query=[PFQuery queryWithClassName:@"Adventures"];
[query whereKey:@"location" nearGeoPoint:self.userLocation withinMiles:100];
[query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {}];

self.userLocation是一个PFGeoPoint。

由于这篇文章,我在 mongo 上添加了一个索引:

{ "userLocation" : "2d" }

我很确定错误来自我在课堂上创建location字段的方式。Adventure我正在使用 pymongo 访问我的 MongoDB 并使用此代码创建location对象:

def createPFGeoPoint():
    for x in Adventures.find():
        if "longitude" in x and "latitude" in x:
            ##x['location']=json.dumps({"__type": "GeoPoint", "coordinates": [x['longitude'], x['latitude']]});
            x['location']=[x['longitude'], x['latitude']];
            ## save is deprecated but insert_one throws an error: duplicate key error collection: database.Adventures 
            Adventures.save(x)

Parse说要创建一个 JSON 对象,例如{"__type": "GeoPoint","latitude": 40.0,"longitude": -30.0}. Mongo说要创建一个 JSON 对象,例如{ type: "Point", coordinates: [ 40, 5 ] }. 尝试上述方法时,它以字符串类型出现,我很确定它需要是坐标数组,因为:

当我使用 IOS Parse 框架上传 PFGeoPoint 时,它以数组的形式出现在此处输入图像描述

当保存location为这样的数组[-71.12345, 50.12345]时,我在客户端将其打印出来,并且位置对象被识别为 PFGeoPoint。所以我假设我正确地创建了它们。

最后在服务器端我收到这个错误:

错误:解析错误:MongoError:未知运算符:$maxDistance

仅查询Adventure类会返回结果。查询只是nearGeoPoint:不返回任何错误。查询nearGeoPoint: withinMiles:返回错误。

标签: mongodbparse-platformpfquerymongodb-geospatial

解决方案


推荐阅读