如何对Firestore进行动态的"where"查询 [英] How to make a dynamic 'where' query to firestore

查看:55
本文介绍了如何对Firestore进行动态的"where"查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的收藏集"abc"看起来像这样:

My collection "abc" looks like this:

abc
  doc1: {
    users: {
      uid1: {
        gender: 'm',
        age: '99'
      },
      uid2: ...
      uid3: ...
      ...
    }
  },
  doc2: {
    ...

现在,我想查找用户中具有特定uid密钥的所有文档.(我正在从云函数内部查询)

Now I want to find all docs that have a certain uid key in users. (I'm querying from inside a cloud function)

const uid = context.params.uid

我尝试了以下查询:

db.collection('abc').where(`users[${uid}]`, '>', '').get()
db.collection('abc').where(`users.${uid}`, '>', '').get()
db.collection('abc').where(`users[${uid}].gender`, '>', '').get()
db.collection('abc').where(`users.${uid}.gender`, '>', '').get()

它们将在功能控制台中导致错误,指出它不是有效的字段路径.所以我也尝试过这些

They will cause an error in the functions console saying it's not a valid field path. So I also tried these

db.collection('abc').where(new admin.firestore.FieldPath(`users[${uid}]`), '>', '').get()
db.collection('abc').where(new admin.firestore.FieldPath(`users[${uid}].gender`), '>','').get()
db.collection('abc').where(new admin.firestore.FieldPath(`users.${uid}`), '>', '').get()
db.collection('abc').where(new admin.firestore.FieldPath(`users.${uid}.gender`), '>', '').get()

哪个也失败了.

这是否意味着不可能动态查询?或如何正确执行此操作?我应该以不同的方式构造数据吗?怎么样?

So does that mean a dynamic where query is not possible? Or how do I do this correctly? Should I structure my data differently? How?

推荐答案

第一个示例似乎在语法上被破坏了:

The first sample just seems syntactically broken:

db.collection('abc').where(`users[${uid}]`, '>', '').get()

我认为您正在寻找:

db.collection('abc').where('users.' + uid, '>', '').get()

Firestore在交易中相当有限的支持在文档中具有重复的嵌套值.虽然可以像您一样对地图进行建模,但您可能需要查看一些替代数据模型.

Firestore has fairly limited support in dealing with repeated nested values inside a document. While it is possible to model a map like what you have, you might want to look at some alternative data models.

如果您希望能够查询包含某个UID的文档,建议您将仅包含UID的数组字段添加到您的数据结构中:

If you want to be able to query for documents that contain a certain UID, I'd recommend adding an array field with just the UIDs to your data structure:

doc1: {
  uids: ['uid1', 'uid2', 'uid3']
}

现在,您可以使用 array-contains 运算符,以查找与某个UID关联的所有文档.

Now you can use the array-contains operator to find all documents that are associated with a certain UID.

db.collection('abc').where('uids', 'array-contains', 'uid1')


您可以对要进行相等性检查的所有字段执行相同的操作.本质上,您是将拥有的关联数组转换为多个数学集合.


You could do the same for all fields where you want to do an equality check. You're essentially turning the associate array you have into multiple mathematical sets.

如果要执行范围查询(如要按年龄进行查询),则必须将用户存储在每个文档的子集合中.

If you want to perform range queries, like you're trying to do on the age, you will have to store the users in a subcollection for each document.

abc // collection
  doc1: { // document
    users: { // collection
      uid1: { // document
        gender: 'm',
        age: '99'
      },
      uid2:  // document
      uid3:  // document
      ...
    }
  },
  doc2: { // document
    ...

使用这种结构,您可以在子集合上(或在所有 users 子集合上)进行查询,这允许再次进行范围查询(因为此时 age 不再重复)字段).

With this structure you can then query on the subcollection (or across all users subcollections), which allows range queries again (as age at that point is not a repeated field).

这篇关于如何对Firestore进行动态的"where"查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆