Firebase 9 Firestore Get A Document By ID Using getDoc()
Learn how to get document by ID using the getDoc() method in Firebase version 9 Cloud Firestore Database.
- Get All Documents From A Collection
- Get Document By ID → you’re here
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.