$rand
On this page本页内容
Definition定义
$rand
$rand
returns a random float between 0 and 1.返回一个介于0和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
.$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 }
Select Random Items From a Collection从集合中随机选择项目
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
来进一步细化find
操作。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 }