Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Firestore - How to get several documents by multiple ids in one round-trip?

I am wondering if it's possible to get multiple documents by a list of ids in one round trip (network call) to the Firestore database.

like image 386
Joon Avatar asked Oct 26 '22 17:10

Joon


People also ask

Can firestore update multiple documents matching a condition using one query?

Cloud Firestore does not support the equivalent of SQL's update queries.

Can two documents have the same ID in firestore?

You cannot have several documents with the same ID in one Collection: a document ID must be unique across a collection.


1 Answers

if you're within Node:

https://github.com/googleapis/nodejs-firestore/blob/master/dev/src/index.ts#L978

/**
* Retrieves multiple documents from Firestore.
*
* @param {...DocumentReference} documents - The document references
* to receive.
* @returns {Promise<Array.<DocumentSnapshot>>} A Promise that
* contains an array with the resulting document snapshots.
*
* @example
* let documentRef1 = firestore.doc('col/doc1');
* let documentRef2 = firestore.doc('col/doc2');
*
* firestore.getAll(documentRef1, documentRef2).then(docs => {
*   console.log(`First document: ${JSON.stringify(docs[0])}`);
*   console.log(`Second document: ${JSON.stringify(docs[1])}`);
* });
*/

This is specifically for the server SDK

UPDATE: Cloud Firestore Now Supports IN Queries!

myCollection.where(firestore.FieldPath.documentId(), 'in', ["123","456","789"])
like image 169
Nick Franceschina Avatar answered Nov 09 '22 00:11

Nick Franceschina