如何有条件地检索Firestore中当前用户的DOC字段
我需要有条件地检索当前用户文档的字段。这是我的Firestore DB结构。
我需要所有映射类型对象(setup1,setup2,setup3 ...)及其字段,而别无其他。
如下所示,可以有条件地获取当前用户文档的所需字段:
const userRef = collection(db, 'users');
const q = query(userRef, where('uid', '==', userID), where('config1', '!=', false));
const userDocs = await getDocs(q);
但是我不能这样做,因为用户只有对自己的文档的阅读许可:
match /databases/{database}/documents {
match /users/{uid} {
allow read, write: if request.auth.uid == uid;
}
}
此外,不可能使用“没有“查询”
const userRef = doc(db, 'users', userID); // can't use "where" here
// const userRef = doc(collection(db, 'users'), userID) // same here
const userDoc = await getDoc(userRef);
setUserData(userDoc.data());
:是否可以以某种方式使用“在哪里”或从UserDoc.data()过滤字段?
I need to conditionally retrieve fields of current user's doc. Here is my Firestore db structure.
I need all map type objects (setup1, setup2, setup3...) and their fields, nothing else.
It is possible to conditionally get the needed fields of current user's doc as follows:
const userRef = collection(db, 'users');
const q = query(userRef, where('uid', '==', userID), where('config1', '!=', false));
const userDocs = await getDocs(q);
But I cannot do that as users have only read permission for their own doc:
match /databases/{database}/documents {
match /users/{uid} {
allow read, write: if request.auth.uid == uid;
}
}
In addition, it is not possible to use "where" without "query":
const userRef = doc(db, 'users', userID); // can't use "where" here
// const userRef = doc(collection(db, 'users'), userID) // same here
const userDoc = await getDoc(userRef);
setUserData(userDoc.data());
Is it possible to somehow use "where" above or to filter fields from userDoc.data()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用Firebase Client SDK不可能。您只能获取整个文档。 firestore node.js sdk 确实支持选择所需的字段要在服务器端使用,因此您必须使用云功能或类似的东西。
query(userRef,where('uid','==',userId),where('config1','!=',false) );
此查询不会返回任何文档,因为文档根目录中没有
config1
字段。最好创建一个子收集
设置
。然后,您应该能够运行所有必需的查询。如果每个设置子文档具有
Conflign
字段,则以下查询应返回所有用户设置给定配置为true的地方:That's not possible using the Firebase Client SDK. You can only fetch the whole document. Firestore Node.JS SDK does support selecting required fields only but that is meant to be used on server side so you'll have to use Cloud functions or something similar.
query(userRef, where('uid', '==', userID), where('config1', '!=', false));
This query will not return any document because there's no
config1
field in the root of document.It might best to create a sub-collection
setups
. Then you should be able to run all your required queries.If each setup sub-document has the
configN
field the following query should return all users setups where the given config is true: