May 20, 2021 WeChat Mini Program Development Document
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregation phase. Randomly select a specified number of records from the document.
Sample takes the form of:
sample({
size: <正整数>
})
Note: size is a positive integer, otherwise an error will occur.
Suppose the document users have the following records:
{ "name": "a" }
{ "name": "b" }
If you're in a raffle now, you'll need to select a lucky user. The sample is then called as follows:
db.collection('users')
.aggregate()
.sample({
size: 1
})
.end()
A randomly selected user-corresponding record is returned, with the following results:
{ "_id": "696529e4-7e82-4e7f-812e-5144714edff6", "name": "b" }