Firebase 查询“IN"是否有解决方法?限制为 10 个? [英] Is there a workaround for the Firebase Query "IN" Limit to 10?
问题描述
我有一个 Firebase 查询,其中包含一个大小 > 10 的 ID 数组.Firebase 对一个会话中要查询的记录数有限制.有没有办法一次查询超过 10 个?
<块引用>[未处理的承诺拒绝:FirebaseError:无效查询.'in' 过滤器支持值数组中最多 10 个元素.]
让查询 = config.db.collection(USER_COLLECTION_NAME).where("id", "in",matchesIdArray);const users = await query.get();
(matchesIdArray.length 需要无限制)
我发现这对我来说效果很好,而无需进行尽可能多的查询(循环和批量请求 10 个).
导出函数 getContentById(ids, path) {返回新的承诺((res)=> {//如果集合没有任何 id 或路径,则不要运行if (!ids || !ids.length || !path) return res([]);const collectionPath = db.collection(path);让批次 = [];而(ids.length){//Firestore 将批次限制为 10const batch = ids.splice(0, 10);//将批量请求添加到队列中批量推送(新承诺(响应 => {集合路径.在哪里(firebase.firestore.FieldPath.documentId(),'在',[...批]).得到().then(results => response(results.docs.map(result => ({ ...result.data()}) )))}))}//取完所有数据后,返回Promise.all(batches).then(content => {res(content.flat());})})}
I have a query for firebase that has an array of IDs that has a size > 10. Firebase has restrictions on the number of records to query in one session. Is there a way to query against more than 10 at a time?
[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
let query = config.db
.collection(USER_COLLECTION_NAME)
.where("id", "in", matchesIdArray);
const users = await query.get();
(matchesIdArray.length needs to be unlimited)
I found this to work well for me without needing to make as many queries (loop and request in batches of 10).
export function getContentById(ids, path) {
return new Promise((res) => {
// don't run if there aren't any ids or a path for the collection
if (!ids || !ids.length || !path) return res([]);
const collectionPath = db.collection(path);
let 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(
new Promise(response => {
collectionPath
.where(
firebase.firestore.FieldPath.documentId(),
'in',
[...batch]
)
.get()
.then(results => response(results.docs.map(result => ({ ...result.data()}) )))
})
)
}
// after all of the data is fetched, return it
Promise.all(batches).then(content => {
res(content.flat());
})
})
}
这篇关于Firebase 查询“IN"是否有解决方法?限制为 10 个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!