用于具有多个限制的查询的 NoSQL 数据库设计 (Firebase) [英] NoSQL database design for queries with multiple restrictions (Firebase)

查看:12
本文介绍了用于具有多个限制的查询的 NoSQL 数据库设计 (Firebase)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要最佳实践技巧来提高我的 noSQL 数据库(托管在 Firebase 上)的性能.此外,我需要有关如何构建节点的提示.

I need best practice tips to enhance the performance of my noSQL database (hosted on Firebase). Moreover, I need tips on how to structure the nodes.

数据库存储产品信息,具有三个主要属性:

The database stores product information, with three main properties:

$productId          
     /date           
     /category           
     /subcategory

在我的网站上,我有三个视图:

On my website, I have three views:

  • 检索最后 4 件产品(orderBy date)
  • 检索类别 X 的最后 4 个产品(按日期)
  • 检索类别 X 和子类别 Y 的最后 4 个产品(按日期).

请注意,我还有一个节点 product_images,其中包含与 productID 匹配的子节点.所以构建数据库如下:

Note that I also have a node product_images, which have subnodes matching the productIDs. So constructing the databas as follows:

$categoryId
        $subCategoryId 
               $productId

将不起作用,因为我需要事先知道 $categoryId 和 $subCatrgoryId,然后才能将其与 $productId 匹配.这也使得检索最后 4 个产品变得困难.

will not work as I need to know beforehand the $categoryId and $subCatrgoryId before I can match it with $productId. It would also make it difficult to retrieve the last 4 products.

我将如何以有效的方式构建我的 noSQL 数据库,以及如何使用 Firebase 检索具有多重限制的产品?

How would I construct my noSQL database in an efficient way, and how would I retrieve the products with Firebase filtering it out with multiple restrictions?

我知道在 Firebase 中您可以使用 orderByChild 和 equalTo,但这仅适用于一个限制,而我必须处理一到三个.

I know that in Firebase you can use orderByChild and equalTo, but this only works on one restriction, whereas I have to deal with one to three.

推荐答案

由于 Firebase 只能过滤一个属性,因此您必须将要过滤的值组合到一个属性中.由于您有多种过滤需求,因此您可能需要为每个过滤用例使用这样的属性.

Since Firebase only can filter on one property, you'll have to combine the values you want to filter on into a single property. Since you have multiple filtering needs, you might need such a property for each filtering use-case.

如果您将 <category>_<date> 组合成一个属性,我们将其称为 multi-prop,这对您来说似乎是可能的.我们还将 <category>_<subcategory>_<date> 组合成一个名为 megaprop 的属性:

It seems that this will be possible for you if you combine <category>_<date> into a single property, which we'll call multi-prop. We'll also combine <category>_<subcategory>_<date> into a property called megaprop:

$productId          
    /date           
    /category           
    /subcategory
    /multiprop
    /megaprop

一些样本数据:

id_cc_1234
    date: 20151031
    category: candy
    subcategory: halloween
    multiprop: candy_20151031
    megaprop: candy_halloween_20151031
id_tg_2345
    date: 20151125
    category: candy
    subcategory: thanksgiving
    multiprop: candy_20151125
    megaprop: candy_thanksgiving_20151125
id_tg_3456
    date: 20151125
    category: food
    subcategory: thanksgiving
    multiprop: food_20151125
    megaprop: food_thanksgiving_20151125
id_sk_4567
    date: 20151205
    category: candy
    subcategory: sinterklaas
    multiprop: candy_20151205
    megaprop: candy_sinterklaas_20151205
id_sc_5678
    date: 20151225
    category: candy
    subcategory: christmas
    multiprop: candy_20151225
    megaprop: candy_christmas_20151225

现在您的查询变成:

  1. 检索最近的 4 个产品

  1. retrieve the last 4 products

ref.orderByChild('date').limitToLast(4);

  • 检索类别 X 的最后 4 个产品

  • retrieve last 4 products of category X

    ref.orderByChild('multiprop').startAt('candy').endAt('candy_~').limitToLast(4);
    

  • 检索类别 X 和子类别 Y 的最后 4 个产品.

  • retrieve last 4 products of category X and subcategory Y.

    ref.orderByChild('megaprop').startAt('candy_christmas').endAt('candy_christmas_~').limitToLast(4);
    

  • 另见:http://jsbin.com/piluzo/edit?js,console 用于工作版本.

    Also see: http://jsbin.com/piluzo/edit?js,console for a working version.

    这篇关于用于具有多个限制的查询的 NoSQL 数据库设计 (Firebase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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