Lets see how to query Firebase.

First thing, when we do query, 'index' will always help, for both SQL and NoSQL.

In Firebase, we can also set index on props, for example we want to set an 'index' on 'courses' --> 'url' prop, we will use 'url' to locate course object.

How to set up index?

In Firebase console --> database --> Rules:

We tell Firebase, for 'courses' document, we want to set index on 'url'.

After setting up index, then how can we do query?

  findCourseByUrl(courseUrl): Observable<Course>{
return this.angularFire.database.list('courses', {
query: {
orderByChild: 'url',
equalTo: courseUrl
}
})
.map((courses) => courses[]); // get courses document which url = courseUrl
}

We need to pass 'query' object for searching course. Notice we are using 'orderByChild' and 'equalTo'.

Get courses' lessons:

  findAllCourseLessons(courseUrl){
const course$ = this.findCourseByUrl(courseUrl); const lessonsPreCourse$ = course$
.filter(course => !!course)
.switchMap((course) => {
console.log(course);
return this.db.list(`lessonsPerCourse/${course.$key}`)
}); return lessonsPreCourse$
.map((lessonKeys) => lessonKeys
.map( (lessonKey) => {
return this.db.object(`lessons/${lessonKey.$key}`)
}))
.flatMap((res) => {
return Observable.combineLatest(res);
}); }

We have a document 'lessonsPreCourse' to maintain the lessons which course includes:

This is normalize the dataset, to avoid nest array.

After getting 'lessonsPreCourse', we are going to get all the lessons in 'lessons' document.

Then in the UI, we can use 'async' pipe to show the lessons:

<md-list>
<md-list-item *ngFor="let lesson of lessons$ | async">
<a *ngIf="lesson.hasVideoUrl" [href]="lesson.videoUrl">{{lesson.description}}</a>
<span *ngIf="!lesson.hasVideoUrl">{{lesson.url}}</span>
</md-list-item>
</md-list>
  ngOnInit() {
if(this.route.snapshot.params['url']){
const url = this.route.snapshot.params['url'];
this.lessons$ = this.courseService.findAllCourseLessons(url);
}
}

Github

[AngularFire 2] Joins in Firebase的更多相关文章

  1. [Firebase] 1. AngularFire, $save, $add and $remove, Forge

    Basic angularFire options: $save, $add and $remove. The way connect firebase: var app = angular.modu ...

  2. [AngularFire 2] Object Observables - How to Read Objects from a Firebase Database?

    In this lesson we are going to learn how to use AngularFire 2 to query objects, and read them from t ...

  3. [AngularFire] Firebase OAuth Login With Custom Firestore User Data

    import { NgModule } from '@angular/core'; import { AuthService } from './auth.service'; import { Ang ...

  4. [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor

    The upload class will be used in the service layer. Notice it has a constructor for file attribute, ...

  5. [Firebase] 4. Firebase Object related Database

    The idea: This post we are going to learn how to build a Firebase Forage with object related databas ...

  6. [Firebase] 3. Firebase Simple Login Form

    Using $firebaseSimpleLogin service. Here we use three methods for login, logout, register and getCur ...

  7. [Angular] AuthService and AngularFire integration

    Config AngularFire, we need database and auth module from firebase. import {NgModule} from '@angular ...

  8. [AngularFire 2 ] Hello World - How To Write your First Query using AngularFire 2 List Observables ?

    In this lesson we are going to use AngularFire 2 for the first time. We are going to configure the A ...

  9. SQL Tuning 基础概述07 - SQL Joins

    N多年之前,刚刚接触SQL的时候,就被多表查询中的各种内连接,外连接,左外连接,右外连接等各式各样的连接弄的晕头转向. 更坑的是书上看到的各种表连接还有两种不同的写法, 比如对于表A,表B的查询 1, ...

随机推荐

  1. 移动端meta几个值的设置以及含义

    <!-- 为移动设备添加 viewport --> <meta name="viewport" content="width=device-width, ...

  2. js全局的解析与执行过程

    先看下面实例的执行结果: alert(a);//undefined alert(b);//报错 alert(f);//输出f函数字符串 alert(g);//undefined var a = 1; ...

  3. centos配置tomcat编辑修改

    https://jingyan.baidu.com/article/6525d4b1382f0aac7d2e9421.html

  4. 使用Vue动态生成form表单

    form-create 表单生成器 具有数据收集.校验和提交功能的表单生成器,支持双向数据绑定和事件扩展,组件包含有复选框.单选框.输入框.下拉选择框等表单元素以及省市区三级联动,时间选择,日期选择, ...

  5. Ubuntu 16.04下的LAMP环境配置

    在学习开发过程中,每当遇到新的问题时,通常都能在网上搜到解决的方法,但是网上的方法千千万,有些是已经过时了的,有些是跟自己开发环境不同的,总是不能第一时间能找到答案. 而当时遇到的问题可能在今后的开发 ...

  6. ps---报告当前系统的进程状态

    ps aux最初用到Unix Style中,而ps -ef被用在System V Style中,两者输出略有不同.现在的大部分Linux系统都是可以同时使用这两种方式的. linux上进程有5种状态: ...

  7. [置顶] Docker学习总结(1)——Docker实战之入门以及Dockerfile(一)

    一.Docker是什么? 首先Docker是软件工业上的集装箱技术 回顾,在没有集装箱出现以前,传统运输行业中,会存在这些问题: 在运输过程中,货物损坏 装卸.运输货物,效率低下 运输手续繁多及运输环 ...

  8. poppler交叉编译

    项目须要生成和打开pdf文件,编译了pdflib库,能够使用.可是上面有水印. ..假设是商业用途去水印的话须要获得许可. 没办法.换呗. 然后就是poppler,这个库的功能非常强大,里面更是增加了 ...

  9. 01-Jvm 内存区域复习笔记

    Java内存区域          1.程序计数器(Program Counter Register)         在虚拟机中一块较小的内存空间.它的作用能够看做是当前线程所运行的字节码的行号指示 ...

  10. 跟着辛星用PHP的反射机制来实现插件

    我的博文的前一篇解说了PHP的反射机制是怎么回事,假设读者还不清楚反射机制,能够搜索下或者看我的博文,都是不错的选择.我们開始解说一下怎么用PHP来实现插件机制.所谓插件机制.就是我们定义一个接口.即 ...