简体   繁体   English

如何在 Firebase 云文档的字段中搜索多个值?

[英]How to search for multiple values within a field of Firebase Cloud documents?

So I'm trying to make a favourites page in my android mobile application.所以我试图在我的 android 移动应用程序中创建一个收藏页面。 The products are in their own top-level collection and structured as:这些产品在他们自己的顶级集合中,结构如下:

{
"Trainers": {
    "trainer1": {
        "brand": "Nike",
        "name": "Air Force 1",
        "colour": "Black"
        "url": ".................."   //FOR IMAGES

    },
    "trainer2": {
        "brand": "Adidas",
        "name": "Yeezy Boost 700",
        "colour": "Black"
        "url": ".................."   
    },
    "trainer3": {
        "brand": "Nike",
        "name": "Air Spiridons",
        "colour": "Cream"
        "url": ".................."   
    }
  }  
 }

I was then going to have another top-level collection that contains documents named as a users id, followed by an array of their favourite trainers 'name'.然后我将拥有另一个顶级集合,其中包含名为用户 id 的文档,后跟一组他们最喜欢的培训师“姓名”。 However, in order to populate the favourites page, I would then need to search the 'Trainer' collection for each shoe in a users favourites document.但是,为了填充收藏页面,我需要在用户收藏文档中搜索每只鞋的“Trainer”集合。

So going back to my question, if User1 has liked 'Air Force 1 & Yeezy Boost 700' (or maybe more products than this) is it possible to query a collection where the documents 'name' field equals multiple values or is there a better way to structure a favourites activity?所以回到我的问题,如果 User1 喜欢“Air Force 1 & Yeezy Boost 700”(或者可能比这更多的产品),是否可以查询文档“名称”字段等于多个值的集合,或者是否有更好的构建收藏夹活动的方法?

is it possible to query a collection where the documents 'name' field equals multiple values or is there a better way to structure a favourites activity?是否可以查询文档“名称”字段等于多个值的集合,或者是否有更好的方法来构建收藏夹活动?

Sure it is, using an array that can contain max 10 values.当然可以,使用最多可以包含 10 个值的数组。 According to the docs:根据文档:

Similarly, use the array-contains-any operator to combine up to 10 array-contains clauses on the same field with a logical OR .同样,使用 array-contains-any 运算符将同一字段上的最多 10 个 array-contains 子句与逻辑OR组合起来。

So in your case, you should use the following query:因此,在您的情况下,您应该使用以下查询:

usersRef.whereArrayContainsAny("name", Arrays.asList("Air Force 1", "Yeezy Boost 700"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM