如何通过过滤数据从Firestore集合中检索数据

时间:2019-04-23 03:18:09

标签: google-cloud-firestore

我正在与angular一起从事google cloud firestore项目。我想通过考虑子集合的属性从Firebase检索数据。您可以在关注我的数据库中看到。

enter image description here

我可以按以下方式检索数据而无需过滤

service.ts

import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Processing } from './processing.model';
import { from } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class ProcessingService {
    patients: Processing;

    constructor(private firestore: AngularFirestore) {}

    /**
    * I'm using this function for retrieve data.
    * It is working
    * I want to filter data using attribute
    */

    getPatients() {
        return this.firestore
        .collection('patients')
        .doc('patientData')
        .snapshotChanges();
    }

}

component.ts

import { Component, OnInit } from '@angular/core';
import { Processing } from './processing.model';
import { ProcessingService } from './processing.service';

@Component({
selector: 'ngx-processing',
styles: [],
template: `
    <ng2-smart-table
    [settings]="settings"
    (createConfirm)="addData($event)"
    (editConfirm)="editData($event)"
    (deleteConfirm)="deleteData($event)"
    [source]="list"
    ></ng2-smart-table>
`
})
export class ProcessingComponent implements OnInit {
    list: Processing[] = [];
    constructor(private service: ProcessingService) {}

    ngOnInit() {
        this.service.getPatients().subscribe(actionArray => {
        let a = actionArray.payload.get('data');
        if (a) {
            this.list = a;
        }
        });
    }

    //I removed the displaying part of the list for your convenient. but it is work. 
    //There is no any error in displaying.

}

我将在上述功能中向您简要介绍。

我使用此函数来检索表中的全部数据。

功能01

getPatients() {
    return this.firestore
    .collection('patients')
    .doc('patientData')
    .snapshotChanges();
}

功能02

 ngOnInit() {
    this.service.getPatients().subscribe(actionArray => {
       let a = actionArray.payload.get('data');

       if (a) {
          this.list = a;
       }

    });
}

我想通过名为level的属性来过滤此数据。然后我要获取数据level= 'p'。那么我如何修改此功能。

0 个答案:

没有答案