How does skipping work in a sharded collection?

50 views
Skip to first unread message

Tomas vd W

unread,
Jan 31, 2012, 4:10:50 AM1/31/12
to mongodb-user
I have a question regarding skipping and sharding. It works perfectly,
but I don't understand how.

I have a simple test setup with 3 shards and a collection sharded by
_id.

When I do a sort/skip/limit operation:

db.mycoll.find().sort({_id: 1}).skip(100).limit(10).explain()

I would expect that each shard would read and send 110 records to
mongos, and that mongos would perform the skip while zipping these 330
records together. But explain() shows that each shard reads and sends
only 10 records to mongos. How do the individual shards know how much
to skip?

Furthermore, if they know how much to skip, why do they all need to
send 10 records? What are the 20 redundent records for?

Could someone explain this?

Thanks,
Tomas

Scott Hernandez

unread,
Jan 31, 2012, 10:17:39 AM1/31/12
to mongod...@googlegroups.com
Skip is applied before limit so each shard does that first. Therefore
they all skip the correct and simply return 10. It could be that only
one of the shards returns all results, or that the are interleaved at
mongos depending on the sort.

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

songhe yang

unread,
Feb 2, 2012, 9:04:14 PM2/2/12
to mongodb-user

On Jan 31, 11:17 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> Skip is applied before limit so each shard does that first. Therefore
> they all skip the correct and simply return 10.

You means that the skip operation was executed on mongod ?

But if each mongod skip 100 documents, the result will not correct.

So i think the skip was executed on mongos.


Hopeful more detail explain about it, 3x

> It could be that only
> one of the shards returns all results, or that the are interleaved at
> mongos depending on the sort.
>

Scott Hernandez

unread,
Feb 2, 2012, 9:46:59 PM2/2/12
to mongod...@googlegroups.com
No, it is run on each shard and then aggregated. It is possible that
the result came from just one shard depending on the sort and shard
key.

songhe yang

unread,
Feb 2, 2012, 10:07:27 PM2/2/12
to mongodb-user
How each shard know the skip number ? I think that each shard should
not just skip 100.
Reply all
Reply to author
Forward
0 new messages