hasNext() issue in DBCursor

134 views
Skip to first unread message

Felipe Micaroni Lalli

unread,
Feb 24, 2012, 3:06:41 PM2/24/12
to mongodb-user
Hello guys! Have a look at this line:

https://github.com/mongodb/mongo-java-driver/blob/master/src/main/com/mongodb/DBCursor.java#L504

This _check(); is really necessary? It makes the "skip(...)" enter in
an illegal state (because the _check() function defines the "_it".

For this moment I solved this issue using count() == 0 instead of
hasNext(), but I don't know what is the difference between both.

Thanks so much,

Felipe.

Scott Hernandez

unread,
Feb 24, 2012, 3:08:58 PM2/24/12
to mongod...@googlegroups.com
Do you have an example of the query options you are executing in your
java code? Or do you have an easy way to reproduce this?

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>

Felipe Micaroni Lalli

unread,
Feb 24, 2012, 3:11:54 PM2/24/12
to mongodb-user
DBCursor cursor = collection.find();

if (cursor.hasNext()) {
// ...
}

cursor.skip(10);

The code above do not work due to an illegal state.
The code bellow works fine, and do the same:

DBCursor cursor = collection.find();

if (cursor.count() > 0) {
// ...
}

cursor.skip(10);


On Feb 24, 6:08 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> Do you have an example of the query options you are executing in your
> java code? Or do you have an easy way to reproduce this?
>
> On Fri, Feb 24, 2012 at 3:06 PM, Felipe Micaroni Lalli
>
>
>
>
>
>
>
> <felipe.micar...@gmail.com> wrote:
> > Hello guys! Have a look at this line:
>
> >https://github.com/mongodb/mongo-java-driver/blob/master/src/main/com...

Felipe Micaroni Lalli

unread,
Feb 24, 2012, 3:16:35 PM2/24/12
to mongodb-user
I found the bug (take a look in my first email, please read the code).
But I am not sure why "_check()" is necessary in the function
"_hasNext()".
I could just comment this line but I am not sure about the
consequences.

hugs,
Felipe.


On Feb 24, 6:11 pm, Felipe Micaroni Lalli <felipe.micar...@gmail.com>

Scott Hernandez

unread,
Feb 24, 2012, 3:16:43 PM2/24/12
to mongod...@googlegroups.com
That is not allowed. You cannot skip after you have started to use the
cursor. If you want to do that skip, after using the cursor, you need
to simply call next() ten times on the client.

count() is a separate server operation and not related to the curser
iterator state.


This is the correct usage:

DBCursor cursor = collection.find().skip(10);

Felipe Micaroni Lalli

unread,
Feb 24, 2012, 3:41:29 PM2/24/12
to mongodb-user
I know this, but why should "hasNext" change the iterator state?
That's make no sense for me. Anyway, I just changed "hasNext" to
"count() > 0" (it is REALLY the same thing). I think it is a bug
"hasNext" change the iterator state, it should be a read-only
function.

hugs,
Felipe.

Scott Hernandez

unread,
Feb 24, 2012, 3:45:35 PM2/24/12
to mongod...@googlegroups.com
It is because it has to call the server to get a batch of results and
that takes the cursor into active mode. Until you call the server you
can't know if there are any results like a next one.

count() works because it is a separate operation and has nothing to do
with the query running or the cursor state.

Felipe Micaroni Lalli

unread,
Feb 24, 2012, 4:15:25 PM2/24/12
to mongodb-user
Ok, but seems like a bug, even after your explanation. What operation
is slower? If count() > 0 is good enough, I think you can just change
the current implementation of "hasNext()" to "count() > 0". Why not?

Thanks.

Scott Hernandez

unread,
Feb 24, 2012, 4:28:17 PM2/24/12
to mongod...@googlegroups.com

Both of those run queries/operations on the server and not the same one. Which means more load and more round trps on the server.

It might make more sense if you look at how count works in dbcusor.

You could ask for a feature where skip() simply eats/ignores some number of the documents in memory on the client but that gets confusing to describe wrt performance.

Felipe Micaroni Lalli

unread,
Feb 24, 2012, 4:55:11 PM2/24/12
to mongodb-user
Yes, you are right. And "hasNext()" is == "count() > 0" only in the
first
time, before any "next()" operation. So, I am sorry to my mistake.

Thanks so much!


On Feb 24, 7:28 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> Both of those run queries/operations on the server and not the same one.
> Which means more load and more round trps on the server.
>
> It might make more sense if you look at how count works in dbcusor.
>
> You could ask for a feature where skip() simply eats/ignores some number of
> the documents in memory on the client but that gets confusing to describe
> wrt performance.
> On Feb 24, 2012 4:15 PM, "Felipe Micaroni Lalli" <felipe.micar...@gmail.com>
Reply all
Reply to author
Forward
0 new messages