The realization of the random selection of the winning user function of the lucky draw mini program

choose randomly

If you are going to draw a lottery now, you need to select a lucky user. It can be implemented using the aggregate Aggregate.sample(size: number), then the calling method of sample is as follows:

db.collection('users') .aggregate() .sample({ size: 1 }) .end()

The record corresponding to a randomly selected user is returned, and the result is as follows:

{ "_id": "696529e4-7e82-4e7f-812e-5144714edff6", "name": "b" }

 

to sum up

If you are going to draw a lottery now, you need to select a lucky user. Then using the cloud-developed aggregation method sample, you can easily implement such a scenario.

Guess you like

Origin blog.csdn.net/qq_29528701/article/details/108576087