$rand (aggregation)
On this page本页内容
Definition定义
Behavior行为
Each time 每次调用$rand
is called it will return a floating point value that has up to 17 digits after the decimal point. Trailing 0s are dropped so the actual number of digits may vary.$rand
时,它都会返回一个浮点值,该值在小数点后最多有17位数字。后面的0会被丢弃,因此实际的位数可能会有所不同。
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 }
]
)
We use an aggregation pipeline to update each document with a random donation amount.我们使用聚合管道来用随机捐赠金额更新每个文档。
db.donors.aggregate(
[
{ $set: { amount: { $multiply: [ { $rand: {} }, 100 ] } } },
{ $set: { amount: { $floor: "$amount" } } },
{ $merge: "donors" }
]
)
The first 第一个$set
stage updates the amount
field. $set
阶段更新金额字段。An initial value between 0 and 1 is generated using 使用$rand
. $rand
生成0和1之间的初始值。Then 然后$multiply
scales it upward 100 times.$multiply
将其放大100倍。
The 第二个$floor
operator in the second $set
stage removes the decimal portion from the amount
to leave an integer value.$set
阶段中的$floor
运算符从amount
中删除小数部分,留下一个整数值。
Finally, 最后,$merge
writes the random value created in the previous steps to the amount
field, updating it for each document in the donors
collection.$merge
将前面步骤中创建的随机值写入amount
字段,并为donors
(捐赠者)集合中的每个文档更新该值。
You can view the results with a projection stage:您可以使用投影阶段查看结果:
db.donors.aggregate(
[
{ $project: {_id: 0, donorId: 1, amount: 1 } }
]
)
The projection shows the scaled amounts are now random values in the range from 0 to 99.投影显示缩放量现在是0到99范围内的随机值。
{ "donorId" : 1000, "amount" : 27 }
{ "donorId" : 1001, "amount" : 10 }
{ "donorId" : 1002, "amount" : 88 }
{ "donorId" : 1003, "amount" : 73 }
{ "donorId" : 1004, "amount" : 5 }
Select Random Items From a Collection从集合中随机选择项目
You can use 您可以在聚合管道中使用$rand
in an aggregation pipeline to select random documents from a collection. $rand
从集合中随机选择文档。Consider 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.aggregate(
[
{ $match: { district: 3 } },
{ $match: { $expr: { $lt: [0.5, {$rand: {} } ] } } },
{ $project: { _id: 0, name: 1, registered: 1 } }
]
)
The first pipeline stage matches all documents where the voter is from district 3.第一个管道阶段匹配选民来自第三选区的所有文件。
The second 第二个$match
stage uses $rand
in a match expression to further refine the selection. $match
阶段在匹配表达式中使用$rand
来进一步细化选择。For each document, 对于每个文档,$rand
generates a value between 0 and 1. $rand
生成一个介于0
和1
之间的值。The threshold of 小于(0.5
in the less than ($lt)
comparison means that $expr
will be true for about half the documents.$lt
)比较中0.5
的阈值意味着$expr
对于大约一半的文档是正确的。
In the 在$project
stage the selected documents are filtered to return the name
and registered
fields. There are 7 voters in District 3, running the code selects about half of them.$project
阶段,筛选所选文档以返回name
和registered
(已注册)字段。第三区域共有7张选票,运行代码可选择其中约一半的选票。
{ "name" : "Archibald", "registered" : true }
{ "name" : "Debarge", "registered" : false }
{ "name" : "Humphrey", "registered" : true }