如何查询PointField - MongoEngine

问题描述 投票:0回答:2

我试图在我的

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

下面是我的模型。

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
收藏中有以下条目。

> 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
没有找到相关资料

python mongodb mongoengine
2个回答
2
投票

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

这里最接近的方法是使用

__near
选择器。这接受
meters
中的范围。所以,你可以根据你的要求给出最接近的范围查询。

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

例子:

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

0
投票

试试这个:

Location.objects(geoCoords="...").update(location_name=loc["location_name"], geoCoords=loc["geoCoords"])
© www.soinside.com 2019 - 2024. All rights reserved.