Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7678697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:42:26+00:00 2026-05-31T17:42:26+00:00

I have strange behaviour in Python/PyMongo. dbh = self.__connection__[‘test’] first = dbh[‘test_1’] second =

  • 0

I have strange behaviour in Python/PyMongo.

dbh = self.__connection__['test']
first = dbh['test_1']
second = dbh['test_2']

first_collection_records=first.find()  
second_collection_records=second.find()


index_f=first_collection_records.count() //20 
index_s=second_collection_records.count() //120

i=0
for f in first_collection_records:
    for s in second_collection_records:
         i=i+1
         print i

and it prints only 120 times (1..120) and not 20×120 times. Can someone tell me why it doesn’t iterate through outer collection ? I printed results, it always takes only first of outer and iterates over inner collection. ( I posted counts which I get in code 20 and 120, I tried with xrange and fetch by index but nothing)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:42:27+00:00Added an answer on May 31, 2026 at 5:42 pm

    If you want to iterate second_collection_records for every first_collection_records though, you can use:

    i=0
    for f in first_collection_records:
        second_collection_records.rewind() #Reset second_collection_records's iterator
        for s in second_collection_records:
             i=i+1
             print i
    

    .rewind() resets cursor to a fresh state, enabling you to once again retrieve data in second_collection_records.


    Explanation:

    second.find()
    

    returns a Cursor object, which contains an iterator.

    When an iterator of Cursor reach its end, it no longer return anything.

    thus:

    for f in first_collection_records: #20
    

    actually does iterated 20 times, but since the inner:

    for s in second_collection_records:
    

    already iterated all objects returned, the second time it is called, second_collection_records no longer returns anything, thus the code inside(i=i+1, print…) is not executed.

    You can try it like this:

    i = 0
    for f in first_collection_records:
        print "in f"
        for s in second_collection_records: 
            print "inside s"
    

    You will get a result:

    inside f
    inside s
    inside s
    ...
    inside s
    inside f  <- since s has nothing left to be iterated, 
                 (second_collection_records actually raised StopIteration such in generator),
                 code inside for s in second_collection_records: is no longer executed
    inside f
    inside f
    

    In depth explanation:

    This line:

    for s in second_collection_records: 
    

    the loop here actually works by next() method of Cursor object, as in: calling second_collection_records.next() until second_collection_records raised StopIteration exception (In Python generator and for loop, StopIteration is caught and code inside for loop would not be executed). So in the second til last loop of first_collection_records, second_collection_records.next() actually raised StopIteration for the inner loop, not executing the code.

    We can easily observe this behavior by doing this:

    for f in first_collection_records:
        print "inside f"
        second_collection_records.next()
        for s in second_collection_records:
            print "inside s"
    

    And the result:

    inside f
    inside s
    ...
    inside s
    inside f
    Traceback (most recent call last):
      ... , in next
        raise StopIteration
    StopIteration
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn python and have encountered some strange behaviour. I am experimenting
I have a strange behaviour in my ajax posting. First, I display an anchor
I have a strange behaviour. I am using a rather heavy page (4000 nodes)
Why this two functions have the strange behaviour of when I click on thead,
I have a strange Hibernate behaviour in my program. I have two classes with
I'm hacking my way through learning Flex and have found some strange behaviour. When
I have come across some strange behaviour, and I'm assuming a bug in Firefox,
I have run into a very strange behaviour I can’t make sense of. I
i have strange behaviour of git - push is working, but clone is not
I have a strange behaviour when autowiring I have a similar code like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.