Firebase 9 Firestore Get A Document By ID Using getDoc()

Raja Tamil
2 min readJul 18, 2022

Learn how to get document by ID using the getDoc() method in Firebase version 9 Cloud Firestore Database.

The sample Firestore Database has a cities collection that has four documents in it like in the screenshot below.

Let’s get the first document of the cities collection by id.

In this case: Edmonton document using its ID (2l3bcSGs2vZBIc3RODwp)

Import Firestore Database and de-structure the three methods that we need:

  • getFirestore()Firestore Database
  • doc() → It takes references of database, collection name, and ID of a document as arguments
  • getDoc() → getDoc() query gets data of a specific document from collection based references mentioned in the doc()method.

import { getFirestore, doc, getDoc } from https://www.gstatic.com/firebasejs/9.8.4/firebase-firestore.js";

The import statement uses the CDN version of the Firebase SDK in this example.

--

--

Raja Tamil
Raja Tamil

Written by Raja Tamil

👇click the link and get 80% OFF on the premium web development courses (HTML5, CSS3, JavaScript ES6, Vue JS 3 & Firebase 10 https://softauthor.com

No responses yet