简体   繁体   English

如何从 Cloud Firestore 检索多个文档,但没有重复?

[英]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.所以我的云 Firestore 看起来像这样,因此我使用 qnumber 来确定组成的随机数,然后调用到 db 集合中。

在此处输入图片说明 ] ]

You need to track IDs of retrieved documents and discard random IDs that were already retrieved.您需要跟踪检索到的文档的 ID,并丢弃已经检索到的随机 ID。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检索作为文档放置在 Firebase Cloud Firestore 中的 UID - How do I retrieve the UID's that I have placed as documents in my Firebase Cloud Firestore 如何在云功能中从Firestore访问多个文档 - How to access multiple documents from Firestore in a cloud function 从 Firestore 的多个文档中检索数据 - Retrieve data from multiple documents from Firestore 如何在 Cloud Firestore 查询中加入多个文档? - How to join multiple documents in a Cloud Firestore query? 如何从 Firestore 获取多个文档 - How can i get multiple documents from the firestore 如何使用 Java Script 从云功能中的云 Firestore 获取文档 - How to get documents from cloud firestore in cloud function with Java Script 如何使用云功能返回 firestore 中的每个集合及其文档? - How do I return each collection and its documents in firestore with cloud functions? 如何获取 Firebase Cloud Firestore 中存在/不存在特定字段的文档? - How do I get documents where a specific field exists/does not exists in Firebase Cloud Firestore? 您如何从云 Firestore 中的大型集合中高效地批量删除文档? - How do you performantly bulk delete documents from a large collection in cloud firestore? 如何在 Firestore 中检索引用的文档? - How to retrieve referenced documents in Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM