Firestore的使用包含多个字段 [英] Firestore use contains on multiple fields

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

问题描述

我有一个即将推出的视频游戏应用.一个游戏发行版可以在多个平台上发行.听说Firestore在如何检索数据方面比Firebase实时数据库灵活得多.我坚持如何检查发布集合中的游戏发布文档是否包含用户选择的平台,以便该应用可以显示在他的平台上发布的游戏.

I have an upcoming video games app. A game release can come out on multiple platforms. I heard that firestore is much more flexible than firebase real time database on how you can retrieve your data. I'm stuck on how can I check if my game release documents in my release collection contains the user chosen platforms, so the app can show the games coming out on his platforms.

这是我目前拥有的

平台是包含平台ID

  databaseReference.collection(getRegionNode())
            .whereEqualTo("m_y", monthFilter)
            .whereArrayContains("platforms", platforms)
            .orderBy("date", Query.Direction.ASCENDING).get().addOnCompleteListener(listener);

以下是游戏发行文档的示例:

Here's an example of a game release document:

   1369: { 
        "src": "Images/dead.png",
        "name": "red dead 2",
        "date": 2018-10-26,
        "region": worldwide,
        "platforms": "[12, 13, 54]"
    }

例如,假设用户只想显示平台12和13的游戏,我想要一个查询,该查询检查并检索12和13在其 平台 列表.谢谢!

Let's say for example, user wants to only be shown platform 12 and 13 games, I want a query that checks and retrieves all releases documents where 12 and 13 are in their platforms list. Thank you!

推荐答案

Firestore查询的

Firestore Query's whereArrayContains(String field, Object value):

使用附加的过滤器创建并返回一个新的查询,该过滤器的文档必须包含指定的字段,该值必须是一个数组,并且该数组必须包含提供的值.

Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.

根据您的注释,作为该方法的第二个参数传递的 platforms 对象的类型为 array .您实际上正在做什么,您正在搜索 platforms 属性,该属性是array类型的数组,由于数据库中的platform数组包含数字,所以这是不可能的:

According to your comments, your platforms object that is passed as the second argument to this method is of type array. What you are actually doing, you are searching in the platforms property which is of type array for an array, which is not possible since the platforms array in your database contains numbers:

"platforms": "[12, 13, 54]"

数组.这样的查询:

databaseReference.collection(getRegionNode())
        .whereEqualTo("m_y", monthFilter)
        .whereArrayContains("platforms", 12) //Passed a number as the second argument
        .orderBy("date", Query.Direction.ASCENDING).get().addOnCompleteListener(listener);

会很好用,因为我们正在 platforms 数组中搜索一个数字.另请注意,如果您打算使用此查询之王,则需要 index .有关如何创建索引的信息,请参见此 post .

Will work fine because we are searching within the platforms array for a number. Please also note, if you intend to use this king of query, an index is required. For how to create an index, please see my answer from this post.

即使使用上述查询,也可以仅使用一个 whereArrayContains()方法调用来过滤项目.如果您将使用多个,则将发生以下错误:

Even if you using the above query, you can filter your items using only one whereArrayContains() method call. If you will use more than one, the following error will occur:

原因:java.lang.IllegalArgumentException:无效的查询.查询仅支持具有单个包含数组的过滤器.

Caused by: java.lang.IllegalArgumentException: Invalid Query. Queries only support having a single array-contains filter.

如果需要在多个平台上进行过滤,则需要通过为拥有的每个平台创建一个属性并链接 whereEqualTo()方法来更改构建数据库的逻辑.电话.我知道这听起来有些怪异,但这就是Cloud Firestore的工作方式.

If you need to filter on more than one platform, you'll need to change the logic of structuring your database by creating a property for each individual platform that you have and chain whereEqualTo() method calls. I know it sounds a little weird but this is how Cloud Firestore works.

您的架构应如下所示:

1369: { 
    "src": "Images/dead.png",
    "name": "red dead 2",
    "date": 2018-10-26,
    "region": worldwide,
    "platformsOne": 12,
    "platformsTwo": 13,
    "platformsThree": 54
}

要查找平台 12 13 54 的所有游戏,应使用类似以下的查询:

To find all the games for platform 12, 13 and 54, you should use a query that looks like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.
    .whereEqualTo("platformsOne", 12)
    .whereEqualTo("platformsTwo", 13)
    .whereEqualTo("platformsThree", 54);

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

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