2

Please apologize me if my question is naive, I am new to python and I am trying my hand on using the collections using pymongo. I have tried to extract the names using

collects = db.collection_names(); #This returns a list with names of collections

But when I tried to get the cursor using

cursor = db.collects[1].find(); #This returns a cursor which has no reference to a collection. 

I understand that the above code uses a string instead of an object. So, I was wondering how I could accomplish this task of retaining a cursor for each collection in the DB, which I can use later to perform operations of search and update etc.

2 Answers 2

2

If you are using the pymongo driver you must use the get_collection method or a dict-style lookups instead. Also you may want to set the include_system_collections to False in collection_names so you don't include system collections (e.g system.indexes)

import pymongo

client = pymongo.MongoClient()
db = client.db
collects = db.collection_names(include_system_collections=False)
cursor = db.get_collection(collects[1]).find()

or

cursor = db[collects[1]].find()
Sign up to request clarification or add additional context in comments.

Comments

1

Sry, i can't create a comment yet, but have you tried?:

cursor = db.getCollection(collects[1]).find();

1 Comment

If @Friwi's answer was satisfactory it is customary to accept the answer. One because the answer helped you, second so that other users know that this question has been resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.