【发布时间】:2016-12-12 08:54:57
【问题描述】:
这是我在“/articles”中的 Firebase 数据库,里面有很多文章。用户可以(使用他/她自己的文章)列出与某些条件相对应的其他文章。为了使文章通过查询测试,它必须是用户文章在“tradableCategories”中列出的类别,同时该文章还需要在其“tradableCategories”中包含用户文章的类别。
这是数据库结构:
"articles": {
"article1": {
"title": "Car",
"category": "vehicles",
"owner": "user1",
"tradableCategories": {
"furnishings": true,
"other": true,
"vehicles": true
},
"category_tradableCategories": {
"vehicles_furnishings": true,
"vehicles_other": true,
"vehicles_vehicles": true
}
},
"article2": {
"title": "Bike",
"category": "vehicles",
"owner": "user2",
"tradableCategories": {
"furnishings": true,
"other": true
"vehicles": true,
},
"category_tradableCategories": {
"vehicles_furnishings": true,
"vehicles_other": true,
"vehicles_vehicles": true
}
},
"article2": {
"title": "Couch",
"category": "furnishings",
"owner": "user2",
"tradableCategories": {
"furnishings": true,
"other": true,
"vehicles": true
},
"category_tradableCategories": {
"furnishings_furnishings": true,
"furnishings_other": true,
"furnishings_vehicles": true
}
},
...
}
user1 拥有 article1,它想要查找家具、其他和车辆中的文章。符合条件的文章也要查找article1的设置类别。使用 SQL 可以轻松完成查询:
SELECT *
FROM articles
WHERE category = ’vehicles’ /* This is article1’s category */
AND find_in_set(category, :tradableCategories) /* :tradableCategories is a stringified, comma-separated set of article1’s tradableCategories: “furnishings,other,vehicles” */
AND NOT owner = ‘user1’
正如您在数据库结构中看到的那样。我已经包含了另一个名为“category_tradableCategories”的对象。我在 Stack Overflow 上看到了各种答案,这些答案解释了如何将两个条件合二为一来搜索项目。这本来可行,但意味着我必须启动 3 个 Firebase 查询,因为我无法在 tradableCategories 中组合三个(或更多)不同的类别。
恐怕这对 Firebase 来说太复杂了,但如果有任何有效的解决方案,我希望得到一些帮助。谢谢!
【问题讨论】:
标签: firebase firebase-realtime-database nosql