【问题标题】:How do you query data by with multiple conditions or indexes?如何通过多个条件或索引查询数据?
【发布时间】:2017-08-31 19:42:59
【问题描述】:

我正在尝试为我的 Firebase 应用程序设计一个高性能的 NoSQL 架构,但是我将如何查询具有唯一标记的多个检查点?

考虑以下规则:

  • 层可以有多个标签
  • 标签可以有多个检查点
  • 标签键(名称)对于每一层都是唯一的
  • 检查点可以有多个标签,但每层只有一个标签

这是我目前所拥有的:

{ 
    "tiers": {
        "1": {
            "web": true,
            "android": true
        },
        "2": {
            "basics": true,
            "angular2": true,
            "aurelia": true
        },
        "3": {
            "basics": true,
            "components": true
        }
    },
    "tags" : {
        "1": {
            "web": {
                "name": "Web Development",
                "children": {
                    "front-end": true,
                    "angular2": true,
                    "aurelia": true
                }
            },
            "android": {
                "name": "Android Development",
                "children": {
                    "front-end": true
                }
            }            
        },
        "2": {
            "front-end": {
                "name": "Basics",
                "parents": {
                    "web": true,
                    "android": true
                },
                "children": {
                    "angular2": true,
                    "aurelia": true,
                    "android-studio": true
                }                
            }                           
        },
        "3": {
            "angular2": {
                "name": "Angular 2.x",
                "parents": {...},
                "children": {...}
            },
            "aurelia": {
                "name": "Aurelia 1.x"
            }   
        }        
    },
    "checkpoints": {
        "<randomKey>" : {
            "name": "Angular 2 Quick Start",
            "tags": {
                "1": "web",
                "2": "front-end",
                "3": "angular2"
            }
        }
    }
}

现在我可以查询第 1 层 web 标记下的所有检查点:

ref.orderByChild('tags/1').equalTo("web").once('value', snap =&gt; console.log(snap.val()));

但由于您只能定义一个indexOn 规则,因此它没有被优化。至少如果我可以设置indexOn 规则,我至少可以过滤掉大部分检查点,然后在我的代码中过滤其余的。

如何根据多个tags 和tiers 有效地查询我的检查点?

最终我需要使用"tags": {"1": "web" AND "2": "front-end"} 查询checkpoints,我终生无法弄清楚如何有效执行。我正在考虑使用复合键创建另一个表(例如,每个 tier/tag 包含对所有子 checkpoints 的引用),但这将导致我需要在每一层中添加和删除引用。

一定有更好的办法。

【问题讨论】:

    标签: firebase firebase-realtime-database nosql


    【解决方案1】:

    我过于复杂化了解决方案 - 我最终:

    1. 从等式中删除Tier
    2. 在与之相关的每个标签中添加每个检查点
    3. 查询与每个选中标签相关的所有检查点,并删除没有出现在每个选中标签中的检查点

    这是我的新架构:

    allTags = [
      {"abc": true, "def": true, "hij": true},
      {"abc": true, "def": true}
    ];
    
    tags: [
      { "0": [
        {"software-development": {name: "Software Development", checkpoints: [ {"abc": true}, {"def": true}, {"hij": true}]}}
      ]},
      {"1": [
        {"web": {name: "Web", checkpoints: [ {"abc": true}, {"def": true}]}},
        {"android": {name: "Android", checkpoints: [{"hij": true}]}}  
      ]}
    ];
    
    checkpoints: [
      {"abc": { name: "Angular2 Quick Start"}},
      {"def": { name: "Building global directives in Angular2"}},
      {"hij": { name: "Android Quick Start"}},
    ];
    

    创建一个新的检查点:

    public createCheckpoint(tags: any[], checkpoint: any) {
      // push checkpoint
      let checkpointRef = this.firebase.child('test/checkpoints');
      let checkpointKey = checkpointRef.push(checkpoint).key(); // might have to do separate call
    
      // Add checkpoint under each related tag
      tags.forEach(tag => {
        let tagRef = this.firebase.child('test/tags/' + tag.tier + '/' + tag.key + '/checkpoints/' + checkpointKey);
        tagRef.set(true)
      });
    }
    

    根据所选标签检索所有检查点:

    public getCheckpointss(tags: any[]) {
      // tag.tier, tag.key
      let checkpointKeysToGet: string[] = [];
      tags.forEach(tag => {
        let ref = this.firebase.child('test/tags/' + tag.tier + '/' + tag.key + '/checkpoints');
        ref.once('value', snap => {
          let tagKeys:string[] = this.convertToArray(snap.val());
          if (checkpointKeysToGet.length == 0){
            checkpointKeysToGet = checkpointKeysToGet.concat(tagKeys);
          }
          else {
            checkpointKeysToGet.forEach(existingTagKey => {
              let tagKeysInBothTiers = tagKeys.filter(tagKey => {
                return checkpointKeysToGet.includes(tagKey, 0)
              });
              checkpointKeysToGet = tagKeysInBothTiers;
              console.log(checkpointKeysToGet);
            });
    
          }
        });
      });
    }
    

    欢迎所有有效的批评,只要您提出解决方案即可:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-13
      • 2012-02-01
      • 2011-07-11
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      相关资源
      最近更新 更多