dimanche 3 juin 2018

Mongoose; Find three random unique records

I have found articles explaining how to find three random records from a database with Mongoose, however, I can't seem to find any articles explaining how to three unique records. I need help!

As of now, my front end checks if it has found any records that are the same. However, this is inefficient and I was wondering if there is a way of doing it directly in the back end.

Here is my current code:

const findPost = async ({ categoryId })=> {
Post.find().count().exec(count=> {
    const random = Math.floor(Math.random() * count)
    return Post.find()
        .skip(random)
        .limit(1)
   })
}




Aucun commentaire:

Enregistrer un commentaire