10。Firebase 对一个 session 中要查询的记录数有限制。有没有办法一次查询超过 10 个? [Unhandled pr-6ren">
gpt4 book ai didi

javascript - Firebase 查询 "IN"限制为 10 是否有解决方法?

转载 作者:行者123 更新时间:2023-12-03 16:12:55 25 4
gpt4 key购买 nike

我有一个对 Firebase 的查询,它的 ID 数组的大小 > 10。Firebase 对一个 session 中要查询的记录数有限制。有没有办法一次查询超过 10 个?

[Unhandled promise rejection: FirebaseError: Invalid Query. 'in' filters support a maximum of 10 elements in the value array.]



https://cloud.google.com/firestore/docs/query-data/queries

enter image description here
  let query = config.db
.collection(USER_COLLECTION_NAME)
.where("id", "in", matchesIdArray);
const users = await query.get();

(matchesIdArray.length 需要是无限的)

最佳答案

我发现这对我来说效果很好,不需要进行尽可能多的查询(循环和请求,每批 10 个)。

export async function getContentById(ids, path) {
// don't run if there aren't any ids or a path for the collection
if (!ids || !ids.length || !path) return [];

const collectionPath = db.collection(path);
const batches = [];

while (ids.length) {
// firestore limits batches to 10
const batch = ids.splice(0, 10);

// add the batch request to to a queue
batches.push(
collectionPath
.where(
firebase.firestore.FieldPath.documentId(),
'in',
[...batch]
)
.get()
.then(results => results.docs.map(result => ({ /* id: result.id, */ ...result.data() }) ))
)
}

// after all of the data is fetched, return it
return Promise.all(batches)
.then(content => content.flat());
}

关于javascript - Firebase 查询 "IN"限制为 10 是否有解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61354866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com