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 |
---|---|---|---|
Questions on Text Search | 3 | 513 | Apr 2024 |
$searchMeta with $match filters to get totalcount | 3 | 539 | May 2024 |
How to get access to ONLY the object in sub-document where there was a hit (match) in Atlas Search | 5 | 867 | Aug 2024 |
Vector Search limit is not working through Pymongo | 1 | 28 | Oct 2024 |
MongoDB Atlas Search index creation question | 0 | 29 | Dec 2024 |