我有parties个文档,分别有authorusers map .我正在try 查询用户创作的所有文档,或者用户在users映射中的所有文档.

问题是下面的代码只考虑or条件中的usersPath != null子句,我不知道为什么:

const getParties = onCall({}, async (request) => {
  const userPath = new FieldPathAdmin("users", request.auth.token.email);
  return dbAdmin
    .collection("parties")
    .where(
      Filter.or(
        Filter.where(userPath, "!=", null),
        Filter.where("author.uid", "==", request.auth.uid)
      )
    )
    .get();
}

如果我交换这where个条款,那么它只考虑author.uid == request.auth.uid个条款.

导入和设置:

import { initializeApp } from "firebase-admin";
import { getFirestore, Filter, FieldPath } from "firebase-admin/firestore";
import { onCall } from "firebase-functions/v2/https";
const app = initializeApp({...});

当事人文档如下所示:

{
  author: {
    uid: "foo-bar"
    ...
  },
  users: {
    example@test.com: {
      uid: "quux-baz"
      email: "example@test.com",
      joinedAt: 1705951291621
    },
    ...
  }
}

注意:我使用firebase-admin包而不是firebase,因为这是在使用可调用函数的 node 服务器上,而不是在浏览器客户端上.

FireBase-管理员版本11.9.0

推荐答案

我的猜测是您缺少查询所需的复合索引.但我也认为这里有一个反模式,因为您需要为每users.<email>个值定义一个索引-这不会进行伸缩.

与其判断map字段中是否存在特定的子项,我建议添加一个仅包含您要查询的邮箱值的数组字段:

userEmails: ["example@test.com"]

现在,您只需要一个索引(在userEmails上,它是自动定义的)就可以找到正确的文档-然后,您可以继续从您已经拥有的users/{email} map 中读取用户数据.

Node.js相关问答推荐

需要关于基于角色授权的设计建议

在我的Next.js应用程序中没有正确设置Process.env.NODE_ENV

如何在mongodb集合中设置数据限制?

Node.js中Redis的并发问题

如何在docker容器上正确安装nodejs?

如何模拟 mysql2 `getConnection`

在 TypeScript 中正确键入 MongoDB find 方法

遇到 - TypeError:try 使用 Express(Node.js) 从 JSON 文件访问数据时无法读取未定义的属性(读取帖子)

TypeScript Eslint警告了一个AWS客户端构造函数(dynamodb),但没有警告另一个(s3)

如何在套接字对象中存储或添加数据?

在 MacOS Ventura 上使用 NVM 安装 node ?

在 React 和 Socket.io 中使用 Effect 钩子重新渲染两次

(Mongoose) 删除 TTL 字段失败

使用 Forms API 进行批量更新时生成 itemId

NestJS 共享模块的问题

使用 mongoose 查找过go 7 天的注册用户总数

在本地运行 Cloud Functions 会出现错误functions.config() 不可用

添加git信息到create-react-app

Node.JS 中的基本 HTTP 身份验证?

如何阻止 babel 将this转换为undefined(并插入use strict)