简体   繁体   中英

How do i retrieve multiple documents from cloud firestore, but no duplicates?

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);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM