I want to randomly pull a documents from a list of document. And currently, it works, but I will receive the same document again, but I don't want to.
let qnumber = Math.floor((Math.random() * 3) + 1);
const dialogflowAgentDoc = db.collection('esequiz').doc(''+qnumber);
So how do I edit it such that I do not pull any duplicates from the random document pulled?
So my cloud firestore looks like this, hence I use qnumber to determine a random number made up, and then called into db collection.
]
You need to track IDs of retrieved documents and discard random IDs that were already retrieved.
Pseudo code:
class UniqueRandomIdProvider()
{
alreadyRetrieved: number[] = [];
public getNewRandomId(): number {
while(true) {
{
const randomId = this.getRandomId();
if (!this.alreadyRetrieved.contains(randomId) {
return randomId;
}
}
}
private getRandomId(): number {
return Math.floor((Math.random() * 3) + 1);
}
}