How to get random record from MongoDB

Radom data is useful to test many scenarios and check our code stability in many ways. We can get random documents from given collection.

Solution 1: Using $sample

Randomly selects the specified number of documents from the input documents. The $sample stage has the following syntax:

{ $sample: { size:  } }

N is the number of documents to randomly select.

From 3.2+ MongoDB, you can get N random docs from a collection using the $sample aggregation pipeline operator:

db.mycoll.aggregate([{ $sample: { size: N } }])

N can be any number.

If you want to select the random document(s) from a filtered subset of the collection, prepend a $match stage to the pipeline:

// Get one random document matching {a: 10} from the mycoll collection.
db.mycoll.aggregate([
    { $match: { a: 10 } },
    { $sample: { size: N } }
])

Note: $sample may get the same document more than once

Solution 2: $rand

$rand returns a random float between 0 and 1. $rand has the following syntax:

{ $rand: {} }

The $rand operator doesn’t take any arguments.

Using query

db.mycoll.find( {$expr: { $lt: [0.5, {$rand: {} }] }})

Using Aggregation (4.4.2+)

db.mycoll.aggregate(
   [   
      { $match: { $expr: { $lt: [0.5, {$rand: {} } ] } } }     
   ]
)

The threshold of 0.5 in the less than ($lt) comparison means that $expr will be true for about half the documents.

The number of documents selected is different each time. If you need to select an exact number of documents, consider using $sample instead of $rand.

Source: Stackoverflow

Leave a Reply