0

I was trying to update PointField in my flask app with upsert_one. But it always inserts new document. I know the problem is with the query which I'm passing.

Below is my model.

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

And the update query.

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

I also tried running get. But I'm getting the error message "Location matching query does not exist." for the below query.

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

I have following entries in my 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" }
>

I couldn't find any related information on querying the PointFiled.

davidism
  • 121,510
  • 29
  • 395
  • 339
James
  • 2,874
  • 4
  • 30
  • 55

2 Answers2

2

To answer to my question. I think there is no way to get the exact points like I have mentioned in the question.

The nearest method works here is to use __near selector. This accepts the range in meters. So, you can give closest range query as per your requirement.

In my case, I gave 100 meters. Which is fine for me.

Example:

Location.objects(geoCoords__near=thelocation["geoCoords"], geoCoords__max_distance=100).upsert_one(location_name=thelocation["location_name"], geoCoords=thelocation["geoCoords"])
James
  • 2,874
  • 4
  • 30
  • 55
0

Try this:

Location.objects(geoCoords="...").update(location_name=loc["location_name"], geoCoords=loc["geoCoords"])
Farhat Nawaz
  • 202
  • 5
  • 20
  • `upsert_one` is not working because `Location.objects(geoCoords=loc["geoCoords"])` returns `Location matching query does not exist.`. So, same for the `update` also. – James Sep 27 '17 at 15:53