1

Here is the script which i have working inside MongoDBs Compass and working inside Studio 3T

db.getCollection("collection").find({
  geometry: {  
    $geoIntersects: {  
       $geometry: {  
          type: "Polygon" ,  
          coordinates: [ [ 
              [-2.4478329206542915, 52.679303638992494], [-2.4423397565917915, 52.677534343091544], [-2.445601322753901, 52.67430779548455], [-2.4509228254394477, 52.676129262942176], [-2.4478329206542915, 52.679303638992494]
          ] ]  
       }  
    }  
  }  
})

and it returns the following results:

enter image description here

As you can see the code seems to work fine, but when I try to run this inside a Python script it keeps failing and I need a little help pointing out the seemingly not so obvious error.

I have changed the fin statement to a simple field find and it works, just not when its a geoIntersects find statement.

However, if I try to do this inside python I keep getting errors.

Please see below for a copy of my python script, sorry for the redacted comments but need to keep data secure.

import pymongo

#mongodb service vars
dbStartc = 'mongodb+srv://'
dbDomain = '@cluster0.o7cud.azure.mongodb.net'
dbUser = 'redacted'
dbPass = 'redacted'
dbName = 'redacted'
dbColl = 'redacted'
dbSettings = '/?retryWrites=true&w=majority'


#test vars
dbName_connected = False
dbColl_connected = False

try:
    #conect to the mongodb instance
    mongoURL = dbStartc + dbUser + ':' + dbPass + dbDomain + dbSettings
    mongocon = pymongo.MongoClient(mongoURL)

    #connect to the database
    dblist = mongocon.list_database_names()
    if dbName in dblist:
        mongocdb = mongocon[dbName]
        #print("MongoDB Service - Database connected")
        dbName_connected = True
        
    #connect to the collection we need
    collist = mongocdb.list_collection_names()
    if dbColl in collist:
        mongocol = mongocdb[dbColl]
        #print("MongoDB Service - Collection connected")
        dbColl_connected = True
    
    #pre checks test
    if dbName_connected and dbColl_connected :
        #print("MongoDB Service - Connection Checks Complete")
        
        find_result = []
        found_count = 0
        
        found_count = mongocol.count_documents( )
        if found_count > 0 :
            print("Collection Document Count:  " + found_count)
            mydoc = mongocol.find({ geometry: { $geoIntersects: { $geometry: { type: "Polygon" , coordinates: [ [ [-2.44783, 52.67930], [-2.44233, 52.67753], [-2.44560, 52.67430], [-2.45092, 52.67612], [-2.44783, 52.67930] ] ] } } } })
            #for x in 
            
            for x in mydoc:
                find_result += [x]
                
            print(find_result)
            
            
 
        
    else :
        print("MongoDB Service - Connection Checks Failed")
            
except Exception as ex:
    print ("Something you were not expecting went wrong!    (" + ex + ")")

Here is the error: enter image description here

Any help in getting python to work would be greatly appreciated

Minty
  • 35
  • 1
  • 8
  • 1
    Python is trying to interpret all your non-quoted tokens as python variables. For example, try `mydoc = mongocol.find({ "geometry": { "$geoIntersects": { "$geometry": { "type": "Polygon" , "coordinates": [ ...` – rickhg12hs Jun 26 '22 at 10:55
  • 1
    Awesome thank you so much, I had a few other errors, but your help was spot on. – Minty Jun 26 '22 at 14:17
  • Can you answer this rather than leaving a comment - the question can be closed then. – Minty Jun 26 '22 at 16:24
  • 1
    This "issue" has been answered many times, for example https://stackoverflow.com/a/66906766/1409374. A Python program must first be valid python. Perhaps the question should be closed as "already answered". – rickhg12hs Jun 26 '22 at 17:20
  • Does this answer your question? [unexpected token '$' in pymongo program](https://stackoverflow.com/questions/66897050/unexpected-token-in-pymongo-program) – rickhg12hs Jun 27 '22 at 04:39

0 Answers0