[英]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.