Take the 2-minute tour ×

In other words, is there a way to rewind it to the beginning?


I am using mongo shell and pymongo.

share|improve this question

2 Answers 2

up vote 7 down vote accepted

The Ruby API has the rewind! method that does exactly what you want.

The Python API also has the cursor.rewind() method.

The PHP API also has the cursor.rewind() method.

However, neither the Java or C++ APIs have the rewind method. All can be found from the official API page.

share|improve this answer
Good to know, but I am not using Ruby. –  mark Mar 26 '12 at 13:29
@mark: added link to python api that has rewind. –  brice Mar 26 '12 at 13:32
I dont know either of those two APIs but I wonder if they are both just driver tricks - Is this something that is supported by MongoDB natively –  Bryan Migliorisi Mar 26 '12 at 13:37
As per the docs and the code the database does not support this natively, and a new query will be sent to the server. –  brice Mar 26 '12 at 13:44

Cursor in pymongo has .rewind() method, you can refer to sample code from previous question with answer that apply.

Native mongo shell api, however, doesn't provide such method, see method help() on DBQuery object prototype.:

> db.collection.find().help()
find() modifiers
        .sort( {...} )
        .limit( n )
        .skip( n )
        .count() - total # of objects matching query, ignores skip,limit
        .size() - total # of objects cursor would return, honors skip,limit
        .showDiskLoc() - adds a $diskLoc field to each returned object

Cursor methods
        .forEach( func )
        .map( func )
share|improve this answer

Your Answer


