1

I'm looping a list and inside that loop I'm looping some documents which are fetched from mongodb. But in the output console I can see only one iteration. But the outer loop works fine. When I debug it goes inside the outer loop but doesn't go to the inner loop. Please help me out.

client = MongoClient('mongodb://localhost:27017/')
db = client['mydb']
documents = icps_db.user.find({})
name_set = ['john', 'marshal', 'ann' ...]

    for name in name_set:
        print(name)
        for idx, document in enumerate(documents):
            print (documents)
            if name in document["filtered_words"]:
                print ("Found " + name)
            else:
                print (name + " not found in document ")   

Output In the second iteration it doesn't reach line : print (str(idx)).

    john
<pymongo.cursor.Cursor object at 0x7faed0ad0910>
Found john
<pymongo.cursor.Cursor object at 0x7faed0ad0910>
john not found in document 
<pymongo.cursor.Cursor object at 0x7faed0ad0910>
Found john
<pymongo.cursor.Cursor object at 0x7faed0ad0910>
john not found in document 
<pymongo.cursor.Cursor object at 0x7faed0ad0910>
john not found in document 
<pymongo.cursor.Cursor object at 0x7faed0ad0910>
Found john
<pymongo.cursor.Cursor object at 0x7faed0ad0910>
john not found in document 
<pymongo.cursor.Cursor object at 0x7faed0ad0910>
Found john
<pymongo.cursor.Cursor object at 0x7faed0ad0910>
Found john
<pymongo.cursor.Cursor object at 0x7faed0ad0910>
john not found in document 
john
marshal
marshal
10
  • 3
    Are you sure documents is not empty? Perhaps print this collection first. Commented Feb 8, 2017 at 15:40
  • documents is probably empty. Commented Feb 8, 2017 at 15:40
  • 1
    documents = icps_db.user.find({}) After you first set of iterations over documents the cursor is used up. It's a read-once container. You either need to cache the results or do find() inside the outer loop. I'm not familiar with mongodb, but a changing the definition of documents to documents = list(icps_db.user.find({})) should make your problem go away. The only way that wouldn't work is if each document has some sort of live callback that uses the cursor (which I doubt). Commented Feb 8, 2017 at 15:52
  • 1
    If that does not work, do documents.rewind() before your inner loop. rewind According to the docs this will "Rewind this cursor to its unevaluated state. Reset this cursor if it has been partially or completely evaluated. Any options that are present on the cursor will remain in effect. Future iterating performed on this cursor will cause new queries to be sent to the server, even if the resultant data has already been retrieved by this cursor." Commented Feb 8, 2017 at 16:04
  • 1
    I have turned my comments into an answer. Commented Feb 8, 2017 at 16:18

1 Answer 1

2

The problem is here:

documents = icps_db.user.find({}) 

After you first set of iterations over documents the cursor is used up. It's a read-once container. You either need to cache the results or do do documents.rewind() before your inner loop.

To cache the results do:

documents = list(icps_db.user.find({})) 

I don't really know MongoDB, so it's possible that each document has some sort of live callback that uses the cursor (I doubt it). If so, simple caching won't work.

The other solution would be to use rewind():

Rewind this cursor to its unevaluated state.

Reset this cursor if it has been partially or completely evaluated. Any options that are present on the cursor will remain in effect. Future iterating performed on this cursor will cause new queries to be sent to the server, even if the resultant data has already been retrieved by this cursor.

Use it like so:

for name in name_set:
    documents.rewind()
    for idx, document in enumerate(documents):
        ...
Sign up to request clarification or add additional context in comments.

Comments

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.