[JAVA-515] Rewind for a DBCursor Created: 02/Feb/12  Updated: 25/Jun/13  Resolved: 25/Jun/13

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Trivial - P5
Reporter: Uladzimir Mihura Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: epam
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

There is no way to rewind cursor in Java.

At the same time other languages supports this:
http://api.mongodb.org/ruby/1.5.2/Mongo/Cursor.html#rewind%21-instance_method
http://api.mongodb.org/python/2.1.1/api/pymongo/cursor.html#pymongo.cursor.Cursor.rewind
http://www.php.net/manual/en/mongocursor.rewind.php



 Comments   
Comment by Jeffrey Yemin [ 25/Jun/13 ]

In 3.0, calling iterator() will return a cursor object.

Comment by Scott Hernandez (Inactive) [ 02/Feb/12 ]

If anything that seems more confusing. Adding a reset() makes sense in this respect across the boards.

For java the current functionality is very java collection like.

It is a little confusing because there is an implicit iterator on DBCursor itself, because it implement next/hasNext().

To do what you want you would need to do something like this:

Iterator<DBObject> iter1 = collection.find().iterator();
iter1.next(); //first
iter1.next(); //second
 
Iterator<DBObject> iter2 = collection.find().iterator();
iter2.next(); //first *possibly a different one since the query runs again.
iter2.next(); //second *see above
 

Comment by Uladzimir Mihura [ 02/Feb/12 ]

Thank you Jeff,

I just want to make driver more developer friendly and standardized.

For example I need to output 3 records and reset cursor to it's original state.
Now it will be done with the following code:

DBCursor c = collection.find();
c.next();
c.next();
c.next();
c = c.copy();

How it can be

DBCursor c = collection.find();
c.next();
c.next();
c.next();
c.rewind();

In Python and PHP rewind leads to reseting all properties of a cursor, even skip and limit.

-vova

Comment by Jeffrey Yemin [ 02/Feb/12 ]

    /**
     * creates a copy of this cursor object that can be iterated.
     * Note:
     * - you can iterate the DBCursor itself without calling this method
     * - no actual data is getting copied.
     *
     * @return
     */
    public Iterator<DBObject> iterator(){
        return this.copy();
    }

So every time you iterate the cursor with new for loop syntax (which will call this method), it creates a copy of the DBCursor, thus resetting it to start at the beginning.

Comment by Uladzimir Mihura [ 02/Feb/12 ]

So I'm a bit confused. Please, consider the following code:

DBCursor c = collection.find();
System.out.printf("current:%s\n", c.curr()); // in the beginning
c.next(); //moving cursor to position 2
c.next();
System.out.printf("current:%s\n", c.curr()); // at 2nd element
for (DBObject o : c) { //iterating from beginning. why?
    System.out.printf("loop:%s\n", o);
}
System.out.printf("current:%s\n", c.curr()); // at 2nd element

Results to:

current:null
current:{ "_id" : 2}
loop:{ "_id" : 1}
loop:{ "_id" : 2}
loop:{ "_id" : 3}
current:{ "_id" : 2}

-vova

Comment by Scott Hernandez (Inactive) [ 02/Feb/12 ]

In java this is done with an iterator implicitly (or explicitly):

DBCursor cursor = db.find(...).limit(...);
 
for(DBObject dbObj : cursor)
   //do something
 
for(DBObject dbObj : cursor)
   //do something else but with a new query/cursor on the server

Generated at Thu Feb 08 08:52:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.