如何有条件地检索Firestore中当前用户的DOC字段

发布于 2025-01-27 02:43:48 字数 872 浏览 2 评论 0原文

我需要有条件地检索当前用户文档的字段。这是我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2025-02-03 02:43:48

我需要所有地图类型对象(setup1,setup2,setup3 ...)及其字段,没有其他。

使用Firebase Client SDK不可能。您只能获取整个文档。 firestore node.js sdk 确实支持选择所需的字段要在服务器端使用,因此您必须使用云功能或类似的东西。

,但是我不能这样做,因为用户只有阅读自己的文档权限:

query(userRef,where('uid','==',userId),where('config1','!=',false) );

此查询不会返回任何文档,因为文档根目录中没有config1字段。

是否可以以某种方式使用上面的“位置”或从UserDoc.data()过滤字段?

最好创建一个子收集设置。然后,您应该能够运行所有必需的查询。

users -> { userId } -> setups -> { setupId }

如果每个设置子文档具有Conflign字段,则以下查询应返回所有用户设置给定配置为true的地方:

const userSetupRef = collection(db, `users/${userId}/setups`);

const q = query(userSetupRef, where('config1', '!=', false));

I need all map type objects (setup1, setup2, setup3...) and their fields, nothing else.

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.

But I cannot do that as users have only read permission for their own doc:

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.

Is it possible to somehow use "where" above or to filter fields from userDoc.data()?

It might best to create a sub-collection setups. Then you should be able to run all your required queries.

users -> { userId } -> setups -> { setupId }

If each setup sub-document has the configN field the following query should return all users setups where the given config is true:

const userSetupRef = collection(db, `users/${userId}/setups`);

const q = query(userSetupRef, where('config1', '!=', false));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文