A simple query to get random documents from the entire collection in stead of sorting.
DB.Model_Name.find( { }, { }, { sort: { something simple here } } );
A simple query to get random documents from the entire collection in stead of sorting.
DB.Model_Name.find( { }, { }, { sort: { something simple here } } );
Hi @Naw_Ing and welcome to the community forum.
If you wish to get a random document from the collection, you can make use of the $sample operator to get specified number of documents.
For example, if you wish to get only one random document, you can write your query as:
db.collection.aggregate([{ $sample: { size: 1 } }])
Please let us know if you have any further questions.
Best Regards
Aasawari
Topic | Replies | Views | Activity |
---|---|---|---|
Search query fails | 6 | 327 | Jul 2024 |
Updating indexes as part of deploy server build process? | 0 | 175 | Jul 2024 |
Create local deployment wih docker failes in docker compose | 0 | 232 | Aug 2024 |
How to index in Atlas Search a string field that store in fact a number | 4 | 83 | Oct 2024 |
mongoDB $lookup with $search pipeline | 0 | 42 | Dec 2024 |