Cursor.skip: How many were actually skipped?

10 views
Skip to first unread message

Tal Liron

unread,
May 1, 2011, 2:18:18 AM5/1/11
to mongod...@googlegroups.com
If I call Cursor.skip(100), but there are only 84 documents in the cursor, I would want to be sure that indeed only 84 were skipped. Does anyone know if it's possible to find this out?

The Cursor.skip API simply returns the Cursor. Are the number of skipped documents counted somewhere else?

(I'm using the Java driver, but I'm assuming it's similar in other drivers.)

Dwight Merriman

unread,
May 1, 2011, 10:15:33 PM5/1/11
to mongod...@googlegroups.com
if there are less documents than the skip value, you should get no results.  however it does not inform you how many there were, but you can assume if no results that there were less than or equal to the skip value as you got no results.

> db.foo.count()
15
> db.foo.find().skip(20)
>
> db.foo.find().skip(20).hasNext()
false
>
> db.foo.find().skip(20).limit(1).hasNext()
false
> db.foo.find().skip(10).limit(1).hasNext()
true
>

--
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.

Antoine Girbal

unread,
May 1, 2011, 11:22:39 PM5/1/11
to mongod...@googlegroups.com
this feature is not available, since mongod does not return any data in
the case that all docs are skipped.
So at this point there would be no place to add this information.
Your best bet is to do a count() in case no document was returned, if
you want to double check.

> --
> 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

> +unsub...@googlegroups.com.

axlfu

unread,
May 2, 2011, 9:07:54 AM5/2/11
to mongodb-user
have you ever read the source code?
actually cursor will run command when you call cur.hasNext() or
cur.next(),try debug

Antoine Girbal

unread,
May 2, 2011, 1:01:46 PM5/2/11
to mongodb-user
Yes I did.
A query command is run, and the response back includes a fixed binary
portion and then a list of bson objects found.
The fixed binary portion is fairly static and all fields are used
already.
There is no bson object if none was found - there can be 1 bson object
representing an error if one occurred.
But the error object would not be a valid place to convey the skipped
number.

Actually there is a bug in driver there - in case server returns an
error that is not a "not master error" the driver returns the error
object as actual result.
opening ticket..

On May 2, 6:07 am, axlfu <freesearc...@gmail.com> wrote:
> have you ever read the source code?
> actually cursor will run command when you call cur.hasNext() or
> cur.next(),try debug
>
> On May 2, 3:22 am, Antoine Girbal <anto...@10gen.com> wrote:
>
> > this feature is not available, since mongod does not return any data in
> > the case that all docs areskipped.
> > So at this point there would be no place to add this information.
> > Your best bet is to do a count() in case no document was returned, if
> > you want to double check.
>
> > On Sun, 2011-05-01 at 01:18 -0500, Tal Liron wrote:
> > > If I call Cursor.skip(100), but there are only 84 documents in the
> > > cursor, I would want to be sure that indeed only 84 wereskipped. Does
> > > anyone know if it's possible to find this out?
>
> > > The Cursor.skip API simply returns the Cursor. Are the number of
> > >skippeddocuments counted somewhere else?

Tal Liron

unread,
May 2, 2011, 2:19:36 PM5/2/11
to mongodb-user
I hope we can have this feature added! I created issue JAVA-344.
Reply all
Reply to author
Forward
0 new messages