On this page本页内容
$rand
returns a random float between 0 and 1.返回0到1之间的随机浮点值。
$rand
has the following syntax:具有以下语法:
{ $rand: {} }
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
.$rand
生成一个介于0和1之间的值,然后使用$multiply
缩放该值。
The $floor
operator removes the decimal portion so the updated amount
is an integer value.$floor
运算符删除小数部分,因此更新的金额是一个整数值。
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 }
The $rand
operator can be used to select random documents from a collection. $rand
运算符可用于从集合中随机选择文档。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.district
字段上的初始匹配选择选民来自选区3的文件。
The $expr
operator uses $rand
to further refine the find
operation. $expr
运算符使用$rand
进一步优化查找操作。For each document, 对于每个文档,$rand
generates a value between 0 and 1. $rand
生成一个介于0和1之间的值。The threshold of 阈值为0.5
means the less than ($lt)
comparison will be true for about half the documents in the set.0.5
意味着集合中大约一半的文档都可以进行小于(($lt)
)的比较。
There are 7 voters in District 3, running the code selects about half of them.第三区有7名选民,运行该代码可以选择其中大约一半的选民。
{ "name" : "Beckham", "registered" : true } { "name" : "Eckhard", "registered" : false } { "name" : "Grimwald", "registered" : true } { "name" : "Humphrey", "registered" : true }