用户创建的字段的Firestore索引 [英] Firestore index for user created fields

查看:37
本文介绍了用户创建的字段的Firestore索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将firestore用作我的本机应用程序的数据库.

I'm using firestore as a database for my react-native app.

我正在我的Firestore上执行以下查询:

I am executing following query on my firestore:

 const docRef = await firebase
        .firestore()
        .collection('meetings')
        .where('visitors.' + user, '==', true)
        .where('meetingTime', '>', yesterday)
        .get()

我收到一条错误消息,告诉我我需要并建立索引,此错误消息是一个可自动创建索引的链接.通过此链接可以看到下一张图片:

I get an error message telling me that i require and index, with this errormessage is a link to create the index automatically. Following this link shows next image:

现在我真的不希望访问者.'user@ test_be'被硬编码在索引中,因为游客对象是动态的,可以将更多的用户添加为访问者,然后索引不再起作用.

Now i really don't want visitors.'user@test_be' to be hardcoded in the index because the visitors object is dynamic, more users can be added as visitors and then the index does not work anymore.

我的会议文档如下:

如何创建索引以确保可以执行查询?还是应该重组我的数据库?还是在Firestore中无法做到?

How can i make an index that makes sure i can execute my query? Or should i restructure my database? Or is this just not possible in firestore?

推荐答案

此问题已得到修复,这要归功于坦率的范puffelen指导我找到正确的文档

This fixed it thanks thanks to frank van puffelen guiding me to the correct docs

我如下更改了Firebase数据结构:

I changed my firebase data structure as follows:

meetings
  meeting
    meetingName: 'meeting 1'
    meetingTime: 1524823200
    visitors: 
      user@user_be: true

更改为

meetings
      meeting
        meetingName: 'meeting 1'
        visitors: 
          user@user_be: 1524823200

然后在我的查询中我这样做:

then in my query i do:

const docRef = await firebase
        .firestore()
        .collection('meetings')
        .where('visitors.' + user, '>', 0)
        .where('visitors.' + user, '>', yesterday)
        .get()

修复链接: https://firebase.google.com/docs/firestore/solutions/arrays

这篇关于用户创建的字段的Firestore索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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