如何查询PointField - MongoEngine

How to query PointField - MongoEngine

我试图用 upsert_one 在我的 flask 应用程序中更新 PointField。但它总是插入新文档。我知道问题出在我传递的查询上。

下面是我的模型。

class Location(db.Document):
    location_name = db.StringField(required=True)
    geoCoords = db.PointField()

以及更新查询。

Location.objects(geoCoords=loc["geoCoords"]).upsert_one(location_name=loc["location_name"], geoCoords=loc["geoCoords"])
#loc["geoCoords"] = [77.6309395,12.9539974]

我也试过运行get。但是我收到以下查询的错误消息 "Location matching query does not exist."

loc = Location.objects(geoCoords=[77.6309395,12.9539974]).get()

我的 location collection.

中有以下条目
> db.location.find()
{ "_id" : ObjectId("59c5019727bae70ad3259e67"), "geoCoords" : { "type" : "Point", "coordinates" : [ 77.6309395, 12.9539974 ] }, "location_name" : "Bengaluru" }
{ "_id" : ObjectId("59c5022d27bae70ad3259ea2"), "geoCoords" : { "type" : "Point", "coordinates" : [ 77.6309395, 12.9539974 ] }, "location_name" : "Bengaluru" }
>

查询PointFiled没有找到相关信息。

试试这个:

Location.objects(geoCoords="...").update(location_name=loc["location_name"], geoCoords=loc["geoCoords"])

回答我的问题。我认为没有办法像我在问题中提到的那样得到确切的分数。

这里最接近的方法是使用 __near 选择器。这接受 meters 中的范围。所以,您可以根据您的要求给出最接近的范围查询。

就我而言,我给了100米。这对我来说很好。

示例:

Location.objects(geoCoords__near=thelocation["geoCoords"], geoCoords__max_distance=100).upsert_one(location_name=thelocation["location_name"], geoCoords=thelocation["geoCoords"])