Home > OS >  How can i get a random user from mongodb?
How can i get a random user from mongodb?

Time:08-31

Hi first of all sorry for asking such a silly question, for a school project i am making a lottery system in MongoDB So i am adding users in my Collection with a lottery id {which is same for every user } , user_id { which is a unique value } , Now i want to fetch a single user Using Lottery id And declare him as a winner How can i do that or is there nay better system i should follow ? i am using python for this ,i Hope that the message i want to convey is clear Thanks For reading , have a good day

CodePudding user response:

Try this:

db.mycoll.aggregate([
        { $match: { lottery_id: 10 } },
        { $sample: { size: 1 } }
    ])

You might want to see this solution for more information.

I hope it helps. :D

  • Related