英文:
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"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论