How to get ID of collection in Firestore with angular Firestore


I’m not able to get the ID of the document when I query a Firestore Database this way :

Could you give me some help ?

import { Component, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection  } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
export interface Item { name: string; }

  selector: 'app-annonces-contactees',
  templateUrl: './',
  styleUrls: ['./'],
export class AnnoncesContacteesPage implements OnInit {
  private annoncesCollection: AngularFirestoreCollection<Item>;
  annonces: Observable<Item[]>;
    constructor(private afs: AngularFirestore) {
      this.annoncesCollection  afs.collection('annonces', ref > ref.where('prix', '>', 1000000))
      this.annonces  this.annoncesCollection.valueChanges();

  ngOnInit() {



I am going to give you an example of how I dot it: Let us suppose I have collection of hospitals and each hospital has its name,phone and location.

  constructor(private firestore:AngularFirestore){}
      this.firestore.collection("hospitals").snapshotChanges().subscribe((data) > {
        this.hospitalsArray > {
          return { id:, location:["location"], number:["phone"], name:["name"]}

“hospitals” is the name of the collection and this id is the id of the document.

So if you want to display in the html file

<ion-item *ngFor"let hospital of hospitalsArray">

Answered By – Charbelalam

Leave a Comment