#How to query random three data ?

18 messages · Page 1 of 1 (latest)

main bane
#

I am trying to query random three query. Is there any built in method to do so ?

noble shellBOT
lavish mulch
#

@main bane Good morning, what do you mean by three?

#

Like 3 random documents?

main bane
#

Yeah

#

random three doc

lavish mulch
#

Well

#

You can query all your docs

#

Then you could do something like

#
// Array
const array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']

const shuffled = array.sort(() => 0.5 - Math.random());
const selected = shuffled.slice(0, 3);

console.log(selected)
#

So this shuffles an array randomly and picks the first 3 items

#

Once you get all your documents from a query, you could filter them using this logic

#

@main bane does that help?

main bane
#

not really. because i don't want to fetch all the data. just random three. anyway maybe i'll have to think about another approach. thanks 🙏

lavish mulch
#

To not get every document, you can get the total doc count via the local API

#

Then get 3 unique random numbers from that number range

#

and query those 3 documents separately

main bane
#

Okay thanks