Firestore allows you to store data in a document format, and documents are grouped into collections.
You can get a document using its id in Firestore using the getDoc(collection(db, “collection_name”, “documentId”))
method.
This tutorial teaches you how to get a document using its id from a collection in Firestore using the getDoc()
method, get a document using its id using the query()
method and get multiple documents using its id.
Table of Contents
Getting a Document Using its Id in Firestore Using getDoc() Method
To get a document using its id,
- Initialize the Firebase app using the Firebase config data
- Get the firestore app as
db
- Create a document reference of the document using the document id as
doc(db, “collection_name”, “document_id”)
- Pass the document ref to the getDoc() method
- It returns the snapshot if the document exists. You can fetch the details of the document using the snapshot.
Code
The following code demonstrates how to get the document with id coimbatore from the collection cities.
import { initializeApp } from "firebase/app";
import { getFirestore, doc, getDoc } from "firebase/firestore";
const firebaseConfig = {
apiKey: “<your_api_key>“,
authDomain: “<your_auth_domain>“,
projectId: “<your_project_id>”,
storageBucket: “<your_Storage_bucket>“,
messagingSenderId: “<sender_id>“,
appId: “<your_app_id>“,
measurementId: “<your_measurement_id>“
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
async function quickstart() {
const docRef = doc(db, "cities", "coimbatore");
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
console.log(docSnap.data());
}
else {
console.log("No such document!");
}
}
quickstart();
Output
{ shortcode: 'cbe' }
Getting a Document Using its Id in Firestore Using query() Method
To get a document using its id using the query()
method,
- Initialize the Firebase app using the Firebase config data
- Get the firestore app as
db
- Create a query object with the document id in the where clause. The
Where
clause column name must bedocumentId()
, and the document IDs to select - Pass the query object to the
getDocs()
method - It returns the snapshot of the selected document. You can access the document from the query snapshot.
Code
The following code demonstrates how to get the document with id coimbatore from the collection cities using the query()
method.
import { initializeApp } from "firebase/app";
import { getFirestore, collection, query, where, getDocs,documentId } from "firebase/firestore";
const firebaseConfig = {
apiKey: “<your_api_key>“,
authDomain: “<your_auth_domain>“,
projectId: “<your_project_id>”,
storageBucket: “<your_Storage_bucket>“,
messagingSenderId: “<sender_id>“,
appId: “<your_app_id>“,
measurementId: “<your_measurement_id>“
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
async function quickstart() {
const q = query(
collection(db, "cities"),
where(documentId(), "in",
[
"chennai"
]
),
);
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
});
}
quickstart();
Output
chennai => { shortcode: 'mas' }
Getting Multiple Documents By ID from FireStore Using getDocs() Method
To get multiple documents by id from Firestore, use the documentId()
function in the query
method.
- For example, to get the documents with the document id chennai and coimbatore, create a query object like
query(collection(db, "cities"), where(documentId(), "in",["chennai","coimbatore”]),)
Code
The following code demonstrates the query method to get documents based on their ids.
import { initializeApp } from "firebase/app";
import { getFirestore, collection, query, where, getDocs,documentId } from "firebase/firestore";
const firebaseConfig = {
apiKey: “<your_api_key>“,
authDomain: “<your_auth_domain>“,
projectId: “<your_project_id>”,
storageBucket: “<your_Storage_bucket>“,
messagingSenderId: “<sender_id>“,
appId: “<your_app_id>“,
measurementId: “<your_measurement_id>“
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
async function quickstart() {
const q = query(
collection(db, "cities"),
where(documentId(), "in",
[
"chennai",
"coimbatore"
]
),
);
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
});
}
quickstart();
Output
chennai => { shortcode: 'mas' }
coimbatore => { shortcode: 'cbe' }