Navigation

$rand

On this page

Definition

$rand

New in version 4.4.2.

$rand returns a random float between 0 and 1.

$rand has the following syntax:

{ $rand: {} }

Examples

Generate Random Data Points

This example models charitable donations. The collection starts with a list of donors.

db.donors.insertMany(
   [
     { donorId: 1000, amount: 0, frequency: 1 },
     { donorId: 1001, amount: 0, frequency: 2 },
     { donorId: 1002, amount: 0, frequency: 1 },
     { donorId: 1003, amount: 0, frequency: 2 },
     { donorId: 1004, amount: 0, frequency: 1 }
   ]
)

Then we construct an operation to update each document with a random donation amount:

db.donors.updateMany(
   {},
   [
      { $set:
         { amount:
            { $floor:
               { $multiply: [ { $rand: {} }, 100 ] }
            }
         }
      }
    ]
)

The empty update filter matches every document in the collection.

For each document we generate a value between 0 and 1 using $rand then scale the value with $multiply.

The $floor operator removes the decimal portion so the updated amount is an integer value.

After updating the collection, the documents look like this:

{ "donorId" : 1000, "amount" : 2, "frequency" : 1 }
{ "donorId" : 1001, "amount" : 58, "frequency" : 2 }
{ "donorId" : 1002, "amount" : 27, "frequency" : 1 }
{ "donorId" : 1003, "amount" : 26, "frequency" : 2 }
{ "donorId" : 1004, "amount" : 42, "frequency" : 1 }

Select Random Items From a Collection

The $rand operator can be used to select random documents from a collection. Given a collection of voter records:

db.voters.insertMany(
   [
     { name: "Archibald", voterId: 4321, district: 3, registered: true },
     { name: "Beckham", voterId: 4331, district: 3, registered: true },
     { name: "Carolin", voterId: 5321, district: 4, registered: true },
     { name: "Debarge", voterId: 4343, district: 3, registered: false },
     { name: "Eckhard", voterId: 4161, district: 3, registered: false },
     { name: "Faberge", voterId: 4300, district: 1, registered: true },
     { name: "Grimwald", voterId: 4111, district: 3, registered: true },
     { name: "Humphrey", voterId: 2021, district: 3, registered: true },
     { name: "Idelfon", voterId: 1021, district: 4, registered: true },
     { name: "Justo", voterId: 9891, district: 3, registered: false }
   ]
)

Imagine you want to select about half of the voters in District 3 to do some polling.

db.voters.find(
   {  district: 3,
      $expr: { $lt: [0.5, {$rand: {} } ] }
   },
   { _id: 0, name: 1, registered: 1 }
)

The intial match on the district field selects documents where the voter is from district 3.

The $expr operator uses $rand to further refine the $find operation. For each document, $rand generates a value between 0 and 1. The threshold of 0.5 means the less than ($lt) comparison will be true for about half the documents in the set.

There are 7 voters in District 3, running the code selects about half of them.

{ "name" : "Beckham", "registered" : true }
{ "name" : "Eckhard", "registered" : false }
{ "name" : "Grimwald", "registered" : true }
{ "name" : "Humphrey", "registered" : true }
←   $comment Update Operators  →