11---
22 title : $rand
3- titleSuffix : Overview of the $rand operation in Azure Cosmos DB for MongoDB (vCore)
3+ titleSuffix : Overview of the $rand operator in Azure Cosmos DB for MongoDB (vCore)
44 description : The $rand operator generates a random float value between 0 and 1.
55 author : suvishodcitus
66 ms.author : suvishod
77 ms.service : azure-cosmos-db
88 ms.subservice : mongodb-vcore
99 ms.topic : language-reference
10- ms.date : 02/12 /2025
10+ ms.date : 09/04 /2025
1111---
1212
1313# $rand
@@ -16,16 +16,130 @@ The `$rand` operator generates a random float value between 0 and 1. This is use
1616
1717## Syntax
1818
19- The syntax for the ` $rand ` operator is as follows:
20-
2119``` javascript
22- { $rand: {} }
20+ {
21+ $rand: {}
22+ }
2323```
2424
2525## Examples
2626
2727Consider this sample document from the stores collection.
2828
29+ ``` json
30+ {
31+ "_id" : " 0fcc0bf0-ed18-4ab8-b558-9848e18058f4" ,
32+ "name" : " First Up Consultants | Beverage Shop - Satterfieldmouth" ,
33+ "location" : {
34+ "lat" : -89.2384 ,
35+ "lon" : -46.4012
36+ },
37+ "staff" : {
38+ "totalStaff" : {
39+ "fullTime" : 8 ,
40+ "partTime" : 20
41+ }
42+ },
43+ "sales" : {
44+ "totalSales" : 75670 ,
45+ "salesByCategory" : [
46+ {
47+ "categoryName" : " Wine Accessories" ,
48+ "totalSales" : 34440
49+ },
50+ {
51+ "categoryName" : " Bitters" ,
52+ "totalSales" : 39496
53+ },
54+ {
55+ "categoryName" : " Rum" ,
56+ "totalSales" : 1734
57+ }
58+ ]
59+ },
60+ "promotionEvents" : [
61+ {
62+ "eventName" : " Unbeatable Bargain Bash" ,
63+ "promotionalDates" : {
64+ "startDate" : {
65+ "Year" : 2024 ,
66+ "Month" : 6 ,
67+ "Day" : 23
68+ },
69+ "endDate" : {
70+ "Year" : 2024 ,
71+ "Month" : 7 ,
72+ "Day" : 2
73+ }
74+ },
75+ "discounts" : [
76+ {
77+ "categoryName" : " Whiskey" ,
78+ "discountPercentage" : 7
79+ },
80+ {
81+ "categoryName" : " Bitters" ,
82+ "discountPercentage" : 15
83+ },
84+ {
85+ "categoryName" : " Brandy" ,
86+ "discountPercentage" : 8
87+ },
88+ {
89+ "categoryName" : " Sports Drinks" ,
90+ "discountPercentage" : 22
91+ },
92+ {
93+ "categoryName" : " Vodka" ,
94+ "discountPercentage" : 19
95+ }
96+ ]
97+ },
98+ {
99+ "eventName" : " Steal of a Deal Days" ,
100+ "promotionalDates" : {
101+ "startDate" : {
102+ "Year" : 2024 ,
103+ "Month" : 9 ,
104+ "Day" : 21
105+ },
106+ "endDate" : {
107+ "Year" : 2024 ,
108+ "Month" : 9 ,
109+ "Day" : 29
110+ }
111+ },
112+ "discounts" : [
113+ {
114+ "categoryName" : " Organic Wine" ,
115+ "discountPercentage" : 19
116+ },
117+ {
118+ "categoryName" : " White Wine" ,
119+ "discountPercentage" : 20
120+ },
121+ {
122+ "categoryName" : " Sparkling Wine" ,
123+ "discountPercentage" : 19
124+ },
125+ {
126+ "categoryName" : " Whiskey" ,
127+ "discountPercentage" : 17
128+ },
129+ {
130+ "categoryName" : " Vodka" ,
131+ "discountPercentage" : 23
132+ }
133+ ]
134+ }
135+ ]
136+ }
137+ ```
138+
139+ ### Example 1: Add a random value
140+
141+ This query adds a random value to each store document.
142+
29143``` javascript
30144db .stores .aggregate ([
31145 {
@@ -38,9 +152,8 @@ db.stores.aggregate([
38152 { $limit: 2 }
39153])
40154```
41- ### Example 1: Add a random value
42155
43- This query adds a random value to each store document. When executed, it might return something like :
156+ The first two results returned by this query are :
44157
45158``` json
46159[
@@ -66,3 +179,4 @@ This query adds a random value to each store document. When executed, it might r
66179## Related content
67180
68181[ !INCLUDE[ Related content] ( ../includes/related-content.md )]
182+
0 commit comments