Appwrite 随机文档列表

问题描述 投票:0回答:2

我目前正在开发 Appwrite,作为我工作的一部分,我面临一个问题。我想随机检索文档。有可能实现吗?谢谢你。

我正在尝试使用限制和偏移。 但没工作

random appwrite
2个回答
0
投票

这是不可能的,除非您要使用特定 id 的过滤器进行查询,然后您需要知道每个 id,或者查询所有 id(循环分页以获取总数),然后从您构建的列表中随机选择。


0
投票

我生成这样的随机文档列表:

const fetchRandomProducts = async () => {
try {
  const responseForTotal = await database.listDocuments(
    "Database key",
    "collection key"
  );
  const randomNum = Math.floor(
    Math.random() * (responseForTotal.total - 5)
  );
  const response = await database.listDocuments(
    "Database Key",
    "Collection Key",
    [Query.limit(5), Query.offset(randomNum)]
  );
  console.log(response);
} catch (error) {}
//

};

© www.soinside.com 2019 - 2024. All rights reserved.