Professional Documents
Culture Documents
Get started
ARCHIVE
WRITE FOR US
STYLE GUIDE
ABOUT
JOB BOARD
Ivano Di Gese
Follow
May 25 · 5 min read
Photo by Patrick Fore on Unsplash
Simple and very frustrating: When you pull out the first 50
documents, you want to be able to paginate data and use $skip and
the $limitcorrectly, showing coherent data in every subsequent
query and avoiding duplicate content.
So if it was purely random, you obviously could extract the same
document in every separated query because every skipped query
won’t care about what happened before and what we sent in
output previously.
That’s why it’s not the solution to our problem because it can’t
consider previous data querying and doesn’t provide any feature to
repeat the query after the first extraction while providing the
ability to avoid duplicate documents.
The real problem of that is how to make the seed related to our
documents field values. Consider these hints and suggestions:
Conclusions
Extracting random documents from a collection could be tricky —
at least as much as the worst aggregates are to build.
And obviously, the seed must be refreshed every time you want to
randomize your data back: That’s why it could be a good idea to
create a new seed value every time you ask for the first page of
results; after that, every subsequent page will use the same seed
value.
Thanks to Zack Shapiro.
Programming
Mongodb
Random
Database
NoSQL
55
WRITTEN BY
Ivano Di Gese
Follow
Passionate IT skills on the run: keep calm, do your stuff and code
better
Better Programming
Follow
Advice for programmers.
Debugging Your iPhone Mobile Web App With Safari Dev Tools
Matthew Croak in Better Programming
Welcome to a place where words matter. On Medium, smart voices and original
ideas take center stage - with no ads in sight. Watch
Make Medium yours
Follow all the topics you care about, and we’ll deliver the best stories for you to
your homepage and inbox. Explore
Become a member
Get unlimited access to the best stories on Medium — and support writers while
you’re at it. Just $5/month. Upgrade
About
Help
Legal
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.