0

I'm running mongo 3.4 (w/ wiredtiger). Up to now I have been using the 'fast pagination' strategy specified in the following article (https://scalegrid.io/blog/fast-paging-with-mongodb), namely:

  1. Retrieve the _id of the last document in the current page
  2. Retrieve documents greater than this “_id” in the next page
//Page 1
db.users.find().limit(pageSize);
//Find the id of the last document in this page
last_id = ...

//Page 2
users = db.users.find({'_id'> last_id}). limit(10);
//Update the last id with the id of the last document in this page
last_id = ...

I am about to shard my collection in order to allow horizontal scaling. As part of enabling sharding, I am going to use a unique composite key (on fields "user_id" and "post_id") for a shard key. This will guarantee document uniquness across shards, and should allow for relatively good document distribution across shards.

But after I shard my collection, will I be able to use the above fast-pagination strategy? If not, is there a common solution?

Thanks

mils
  • 1,878
  • 2
  • 21
  • 42
  • It's `{ "_id": { "$gt": last_id } }`, but the question is basically already answered as a whole. Also, nothing to do with "sharding" and it really should not be. Works as is. – Neil Lunn Nov 08 '17 at 01:56
  • The question of "how" to use forward paging is answered in both the article I linked and the SO question you linked. But *neither* discuss whether or not the sort order is maintained in a sharded cluster. Thx – mils Nov 08 '17 at 01:59
  • Sort order has nothing to do with sharded clusters. Sort order is only ever maintained for "what you actually ask it to return". The is no "guaranteed" sort order. Which is actually explained in that answer, and there is even a [specific duplicate for just the "sort order" part in general](https://stackoverflow.com/questions/11599069/how-does-mongodb-sort-records-when-no-sort-order-is-specified). Not a new thing. – Neil Lunn Nov 08 '17 at 02:01
  • After I shard my collection, will I be able to use the above fast-pagination strategy? – mils Nov 08 '17 at 02:11
  • I actually did say that this has nothing to do with sharding. It makes no difference or should. Yes I do believe I said that. – Neil Lunn Nov 08 '17 at 02:15

0 Answers0