I recently ran into an issue with MongoDB query that I took ~30 mins to figuring out - so I am sharing in case you are facing this at a time - because I didn't notice this, had this issue before even though I have been using MongoDB for a long time.
--
This is that, when implementing a paging mechanism for my sites, I noticed that I have 40 records, however, some of them are duplicates. I checked my items in the DB directly and there is no duplicate, and the total items is 40, this sounds so strange to me for a few minutes.
Turn out, this is the issue with the usage of skip, limit with sort, when using sort function in the executing, MongoDB may return different order in different executions, this is because the items are not stored in a particular order in MongoDB, so with the same sorting mechanism, MongoDB may return the results in any order if there are items with the same values.
This is causing the behavior that you may get the same items in different query/execution but the expected behavior for a paging mechanism is that you do not get the same records for different pages.
--
In my case, there are records with the same createdAt value (bulk insert), and by this, I am very likely to receive duplicates item for different 'page's - page 1 and page 2 should mutually exclusive
--
Solution:
It is a "well"-known behavior (not necessarily be in issue), that when sorting, you should make sure to include at least one field that contains unique values.