pythonmongodbpymongocloud9-ide

MongoDB's find() is not working for specific condition (in aws cloud9)


I am trying to get data from mycollection in cloud9 with following query:

cur = db.mycollection.find({"location":"Hyderabad"})

for i in cur:
    print(i)

But it is not returning anything, no error also (not even in cloud9's terminal in MongoDB console) I have already tried all the syntax available on internet/official documentation for this. I also rechecked my database name, collection name. For same database and collection, find() and find_one(uid) is working fine and returning correct results. On local system also, it is working fine. Any leads are welcomed.Thanks in advance


Solution

  • Make sure you are connecting to the right database. Try this code snippet that uses the mydatabase database.

    from pymongo import MongoClient
    
    db = MongoClient()['mydatabase']
    
    db.mycollection.insert_one({ "uid": "1", "description": "sample", "effect": "allow", "location": "Hyderabad" })
    cur = db.mycollection.find({"location":"Hyderabad"})
    
    for i in cur: print(i)
    

    Result:

    {'_id': ObjectId('5f764a7a7a640074e9012a03'), 'uid': '1', 'description': 'sample', 'effect': 'allow', 'location': 'Hyderabad'}