Joshua Dance
Joshua Dance

Reputation: 10472

Where is the Firebase Admin SDK docs for interacting with Firestore?

The docs for using the Firebase Admin SDK with the Real Time Database are easy to find.

https://firebase.google.com/docs/database/admin/start

I can't find anything similar for how to use the Firebase Admin SDK with Cloud Firestore?

Been scraping for examples of using the Firestore Admin SDK with Firestore online and on StackOverFlow but feel like I am missing something.

For example I need to perform a query on a collection. The documentation for Firestore is great, https://firebase.google.com/docs/firestore/query-data/queries#web-version-9_3, but I can't find documentation on how to do the same with the same query on Firestore with the Admin SDK.

Upvotes: 3

Views: 4566

Answers (1)

Peter Obiechina
Peter Obiechina

Reputation: 2835

To use Admin SDK with firestore, simply follow the Node.js (not Web version 9) instruction in the link you gave.

For example, to perform query on a collection, do something like this:

// const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();

// below is given in the docs link
const citiesRef = db.collection('cities');
const snapshot = await citiesRef.where('capital', '==', true).get();
if (snapshot.empty) {
  console.log('No matching documents.');
  return;
}  

snapshot.forEach(doc => {
  console.log(doc.id, '=>', doc.data());
});

NB: The second link you sent is correct. Simply switch from Web version 9 to Node.js.

Upvotes: 8

Related Questions