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

查看:182
本文介绍了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个产品)

  • 检索类别X的前4个产品(按日期)

  • 检索类别X和子类别Y的前4个产品/ li>
  • retrieve the last 4 products (orderBy date)
  • retrieve last 4 products (by date) of category X
  • retrieve last 4 products (by date) of category X and subcategory Y.

注意,我也有一个节点product_images,它们的子节点与productIDs匹配。所以构造数据库如下:

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

$categoryId
        $subCategoryId 
               $productId

无法工作,因为我需要事先知道$ categoriesId和$ 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 / p>

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天全站免登陆