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. 浏览器加载渲染HTML、DOM、CSS、 JAVASCRIPT、IMAGE、FLASH、IFRAME、SRC属性等资源的顺序总结

    页面响应加载的顺序:   1.域名解析->加载html->加载js和css->加载图片等其他信息 DOM详细的步骤如下: 解析HTML结构. 加载外部脚本和样式表文件. 解析并执行脚 ...

  2. 用AOP改善javascript代码

    Aop又叫面向切面编程,用过spring的同学肯定对它非常熟悉,而在js中,AOP是一个被严重忽视的技术点,这篇就通过下面这几个小例子,来说说AOP在js中的妙用. 1, 防止window.onloa ...

  3. Javascript和jquery事件--事件冒泡和事件捕获

    jQuery 是一个 JavaScript 库,jQuery 极大地简化了 JavaScript 编程,在有关jq的描述中,jq是兼容现有的主流浏览器,比如谷歌.火狐,safari等(当然是指较新的版 ...

  4. Python day4知识回顾

    # -*- coding: utf_8 _*_# Author:Vi#字典是无序的 info = { 'student001':"DIO", 'student002':" ...

  5. 03006_DOS操作数据乱码解决

    1.我们在dos命令行操作中文时,会报错 insert into sort(sid,sname) values(2,"电视机"); ERROR 1366 (HY000): Inco ...

  6. Google、Mozilla、Qt、LLVM 这几家的规范是明确禁用异常的

    作者:陈硕链接:https://www.zhihu.com/question/22889420/answer/22975569来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  7. ThreadLocal使用演示样例

    MainActivity例如以下: package cc.cv; import android.os.Bundle; import android.app.Activity; /** * Demo描写 ...

  8. Angular:了解Typescript

    Angular是用Typescript构建的.因此在学习Angular之前有必要了解一下Typescript. [ 类型 ] Typescript增加了类型系统,好处是: 1. 有助于代码编写,预防在 ...

  9. http 500 Internal Server Error的错误 ajax请求SpringMVC后台中返回500 Internal Server Error

    使用httprequester接口测试能返回数据,但是用ajax返回json格式的时候返回报500Internal Server Error. The server encountered an in ...

  10. matlab 可视化 —— 高级 api(montage)、insertObjectAnnotation、insertMaker

    1. montage:同时显示多个图像 thumbnails = train_x(:, :, :, 1:100); thumbnails = imresize(thumbnails, [64, 64] ...