[英]How to query PointField - MongoEngine
我試圖更新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
任何相關信息。
回答我的問題。 我認為沒有辦法獲得我在問題中提到的確切要點。
最有效的方法是使用__near
選擇器。 這接受以meters
為單位的范圍。 因此,您可以根據需要提供最接近的范圍查詢。
就我而言,我給了100米。 這對我很好。
例:
Location.objects(geoCoords__near=thelocation["geoCoords"], geoCoords__max_distance=100).upsert_one(location_name=thelocation["location_name"], geoCoords=thelocation["geoCoords"])
嘗試這個:
Location.objects(geoCoords="...").update(location_name=loc["location_name"], geoCoords=loc["geoCoords"])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.