2017-09-26 54 views
0

我試圖在我的flask應用程序中更新PointFieldupsert_one。但它總是插入新文件。我知道問題出在我傳遞的查詢上。如何查詢PointField - MongoEngine

下面是我的模型。

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時找不到任何有關信息。

回答

0

要回答我的問題。我認爲沒有辦法像我在問題中提到的那樣得到確切的點。

這裏最接近的方法是使用__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"]) 
+0

'upsert_one'不工作,因爲'Location.objects(geoCoords = LOC [ 「geoCoords」])''返回位置匹配查詢不exist.'。所以,'update'也一樣。 – james