[AngularFire 2] Joins in Firebase
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);
}
}
[AngularFire 2] Joins in Firebase的更多相关文章
- [Firebase] 1. AngularFire, $save, $add and $remove, Forge
Basic angularFire options: $save, $add and $remove. The way connect firebase: var app = angular.modu ...
- [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 ...
- [AngularFire] Firebase OAuth Login With Custom Firestore User Data
import { NgModule } from '@angular/core'; import { AuthService } from './auth.service'; import { Ang ...
- [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, ...
- [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 ...
- [Firebase] 3. Firebase Simple Login Form
Using $firebaseSimpleLogin service. Here we use three methods for login, logout, register and getCur ...
- [Angular] AuthService and AngularFire integration
Config AngularFire, we need database and auth module from firebase. import {NgModule} from '@angular ...
- [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 ...
- SQL Tuning 基础概述07 - SQL Joins
N多年之前,刚刚接触SQL的时候,就被多表查询中的各种内连接,外连接,左外连接,右外连接等各式各样的连接弄的晕头转向. 更坑的是书上看到的各种表连接还有两种不同的写法, 比如对于表A,表B的查询 1, ...
随机推荐
- three.js 运行3D模型
HTML <!DOCTYPE html> <html style="height: 100%;"> <head> <title>m ...
- Redis操作使用规范
Windows 64位操作系统 Redis 安装包(当前教程版本2.8.12) 百度经验:jingyan.baidu.com 方法/步骤 1 在D盘新建文件夹[redis],右键解压Redis Z ...
- python的循环测试
这段代码要实现的是:猜数字58 ,猜三次,三个判断条件和一个结束条件.判断为等于58的时候跳出循环. 首先定义一个固定变量 int 函数是更改input输入的文字类型 if guess_age == ...
- XML学习总结(2)——XML简单介绍
一.XML概念 Extensible Markup Language,翻译过来为可扩展标记语言.Xml技术是w3c组织发布的,目前推荐遵循的是W3C组织于2000发布的XML1.0规范. 二.学习XM ...
- Linux下进程终止过程
不管是在什么系统中,当进程终止之后.系统都须要释放进程占有的资源. 否则.系统资源会被耗尽. 以下将具体说明Linux系统中,进程终止的过程. 进程终止方式 linux的进程终止方式有8种,当中5种是 ...
- 搜索 debian8.7.1 ,google vs baidu
国外的 Linux 比国内流行, debian官方网站只能找到当前版本DVD文件.想找旧版的Debian在百度一圈后徒劳无功,于是把目标转向 google ,只需要输入 debian?8.7.1-i3 ...
- js04---object1
我们目前为止大多数引用类型都是Object类型的实例,Object也是ECMAScript中使用最多的一种类型(就像java.lang.Object一样,Object类型是所有它的实例的基础).Obj ...
- BZOJ2434: [Noi2011]阿狸的打字机(fail树+dfs序)
Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...
- ViewPager+Fragmrnt最简单结合方法
Fragment和ViewPager 本博文系本菜鸟第一次博文展示,有错误之处请虽然提出 FragmentPagerAdapter 谷歌官方提供了这么一个adapter(FragmentPagerAd ...
- Hibernate之API初识及增删改查实现
声明:关于hibernate的学习.非常大一部分东西都是概念性的. 大家最好手里都有一份学习资料,在我的博文中.我不会把书本上的概念一类的东西搬过来.那没有不论什么意义.关于hibernate的学习, ...