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

huangapple go评论72阅读模式
英文:

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

问题

所以我正在尝试在我的Android移动应用程序中制作一个收藏夹页面。产品位于它们自己的顶级集合中,并且结构如下:

{
    "Trainers": {
        "trainer1": {
            "brand": "Nike",
            "name": "Air Force 1",
            "colour": "Black",
            "url": ".................."   //用于图像的URL
        },
        "trainer2": {
            "brand": "Adidas",
            "name": "Yeezy Boost 700",
            "colour": "Black",
            "url": ".................."
        },
        "trainer3": {
            "brand": "Nike",
            "name": "Air Spiridons",
            "colour": "Cream",
            "url": ".................."
        }
    }
}

然后我打算有另一个顶级集合,其中包含以用户ID命名的文档,后面是他们收藏的训练鞋的'名称'数组。然而,为了填充收藏夹页面,我将需要为用户收藏的每双鞋在“Trainers”集合中进行搜索。

回到我的问题,如果用户1喜欢“Air Force 1&Yeezy Boost 700”(或者可能比这更多的产品),是否可能查询一个文档的“名称”字段等于多个值的集合,或者是否有更好的方法来构建收藏夹功能?

英文:

So I'm trying to make a favourites page in my android mobile application. 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'. 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.

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?

答案1

得分: 1

当然可以,可以使用一个最多包含10个值的数组来实现。根据文档的说明:

同样,可以使用array-contains-any运算符,在同一个字段上组合最多10个array-contains子句,逻辑上使用OR

因此在你的情况下,你应该使用以下查询:

usersRef.whereArrayContainsAny("name", Arrays.asList("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. 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.

So in your case, you should use the following query:

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

huangapple
  • 本文由 发表于 2020年4月7日 21:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61081243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定