Create Collection in Database
Get started with the creation of a new collection in your MongoDB, if you do not have one. The collection is a must-have to perform any operation on the database records within MongoDB. Also, the collection must have at least 1 document already inserted in it before applying any command to it. Therefore, we have already created a collection in our MongoDB database. We named it “Dummy” in our “test” database that we will be using in the queries.
Dummy
When working with MongoDB, you must have records to utilize the “rand” operator. The command listed below has been utilized with the “db.Dummy.find()” function that is used to retrieve all documents from a collection “Dummy”. This command returns all documents in the collection, including their unique “_id” field, as well as other fields such as “city”, “count”, and “Desc”. This allows the user to view the data stored in the collection and make decisions about how to work with it.
[{_id: ObjectId("63c8a1f94ff07039aa6cdf6b"), city: 'Texas', count: 3, Desc: 'Mid-sized city' },
{_id: ObjectId("63c8a1f94ff07039aa6cdf6c"), city: 'Los Angeles', count: 3, Desc: 'Mid-sized city'},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6d"), city: 'Italy', count: 10, Desc: 'Most Beautiful and Crowded'},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6e"), city: 'Istanbul', count: 3, Desc: 'Mid-sized city' },
{_id: ObjectId("63c8a1f94ff07039aa6cdf6f"), city: 'Delhi', count: 3, Desc: 'Mid-sized city' }]
Example # 01: $rand Operator
Let us begin with the use of the MongoDB command that is using the aggregate() method on a collection called “Dummy” within the “test” database. The aggregate() method is used to process data from collections and return computed results. The command is using the $project operator, which is used to reshape documents in the pipeline. And it creates a new field called “random” that is assigned the result of the $rand operator.
The $rand operator generates a random number between 0 and 1 (inclusive) for each record in the collection. The {} following the $rand operator indicates that no arguments are being passed to the operator, so it will generate a random number without any specific constraints. The output is a JSON array of documents in the “Dummy” collection, each of which contains an “_id” field and a “random” field. The “random” field contains a random number between 0 and 1 or float-type values that were generated by the $rand operator for all the 5 records also shown in the output below:
[{_id: ObjectId("63c8a1f94ff07039aa6cdf6b"), random: 0.292593749216963},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6c"), random: 0.5529488318506414},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6d"), random: 0.10835699304362681},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6e"), random: 0.702304030840275},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6f"), random: 0.32829452437455164}]
Example # 02: Using $Rand Operator With $Multiply Operator
The first example illustrates the use of the “rand” operator in MongoDB solely on a collection. Now, we will be utilizing it in conjunction with the multiply operator. We have been using the query below for this purpose. It starts with the use of the aggregate function with the $project stage and random operator again. This time the multiply operator has been applied to a “$rand” operator or the value we have from the “Rand” operator is multiplied by 10 to generate a random number between 0 and 10 for each document. The output below shows an array of all the documents in the collection “Dummy”, each with its unique identifier and a new field called random which will be holding the generated random numbers between 0 and 10 i.e., float.
[{_id: ObjectId("63c8a1f94ff07039aa6cdf6b"), random: 9.636797271617377},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6c"), random: 9.612768242636559},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6d"), random: 9.773269856209643},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6e"), r andom: 4.544313454814634},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6f"), random: 3.3338556824055585}]
Example # 03: $Rand Operator With “$Floor operator
The last command illustrates the use of the “rand” operator with the “multiply” operator to generate float-type random numbers. This time, we will elaborate on how to generate integer-type numbers using the “rand” operator in conjunction with the “floor” operator of MongoDB. Therefore, the instruction below has been executed in the MongoDB shell to produce the integer random values for each record in the collection “Dummy”. This time, the random operator is multiplied by 10 to generate a random number between 0 and 10 for each document. After that, the floor operator is applied to round down the random number to the nearest integer.
The output is an array of all the documents in the collection, each with its own unique ObjectId, the city property, and a new property called random which will hold the generated random integer number between 0 and 10 as demonstrated from the output of the instruction as well.
[{_id: ObjectId("63c8a1f94ff07039aa6cdf6b"), city: 'Texas', random: 5},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6c"), city: 'Los Angeles', random: 0},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6d"), city: 'Italy', random: 5},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6e"), city: 'Istanbul', random: 7},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6f"), city: 'Delhi', random: 5}]
Example # 04: $Rand Operator With $Floor Operator
In the illustrations above, we have multiplied out randomly generated values with 10 or nothing to let them in their float-type form or to convert them into an integer value. But, in this example, we will multiply our randomly generated float values with a little bigger figure which is 5000. In the very same way, the command used in the above examples has been utilized here with a slight change. The “multiply” operator multiplies the random number generated by the $rand operator by 5000 to increase the range of the random number. The “floor” operator rounds down the number generated by the $multiply operator to the nearest integer. The output of this instruction below shows 5 records at the MongoDB shell screen with a new property that holds a random integer number between 0 and 5000 as follows:
[{_id: ObjectId("63c8a1f94ff07039aa6cdf6b"), city: 'Texas', random: 2862},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6c"), city: 'Los Angeles', random: 3688},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6d"), city: 'Italy', random: 814},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6e"), city: 'Istanbul', random: 3888},
{_id: ObjectId("63c8a1f94ff07039aa6cdf6f"), city: 'Delhi', random: 2939}]
Conclusion
We have discussed the definition of the “random” operator in MongoDB. Also, we have discussed its uses in the MongoDB command line. Then, we tried 4 of the different examples to demonstrate its use along with other operators. The commands above demonstrate how to use the MongoDB aggregate function and various operators such as $rand, $multiply, and $floor to manipulate data in a collection and generate new properties with random numbers.