1

I am using skip & limit for mongodb C# driver to fetch tickets batchwise like below,

var data = db.collectionName.Find({}).Skip(1000).Limit(500).ToList()

Data fetching is happening as expected. Need confirmation on whether Sort() is mandatory for Skip & limit methods like below ? or sort will be handled by mongodb if not specified

var data = db.collectionName.Find({}).Sort("{_id:1}").Skip(1000).Limit(500).ToList()

I have removed Sort from query to improve time taken to complete fetch operation.

useruser00
  • 157
  • 1
  • 2
  • 14

1 Answers1

1

No, Sort() is not mandatory for Skip() and Limit() methods. You can use them directly like you are using in your query:

var data = db.collectionName.Find({}).Skip(1000).Limit(500).ToList()

To know more about default sort order, refer to below link: https://stackoverflow.com/questions/11599069/how-does-mongodb-sort-records-when-no-sort-order-is-specified#:~:text=When%20we%20run%20a%20Mongo,objects%20in%20forward%20natural%20order.

Saurabh
  • 429
  • 3
  • 10
  • Will there be chances for duplicate records getting fetched as no order provided or will it be based on stored order only – useruser00 Dec 16 '20 at 05:25
  • Duplicate records will not be fetched. The result will be based on the natural order i.e. in the order, the records were found. It may be the same as insertion order but that's not guaranteed. – Saurabh Dec 16 '20 at 05:28
  • 1
    You may get different results for this same "find" command at two different times based on the insertion/update/deletion of documents in the collection. – Saurabh Dec 16 '20 at 05:49