0

I am using Firebase function and Firebase Firestore to develope an API which will store users data.

I wanted to locate the documents using the properties stored in their field. This is the Firebase document which states how to achieve the same.

// Create a reference to the cities collection
var citiesRef = db.collection('cities');

// Create a query against the collection
var queryRef = citiesRef.where('state', '==', 'CA');

I wanted to handle two situations

  1. Where there is no document with the present conditions

  2. Where there are more than two documents with the present conditions

How could the above two situation be handled?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Rajesh K
  • 683
  • 2
  • 9
  • 35
  • For #1, see https://stackoverflow.com/questions/47251919/firestore-how-to-perform-a-query-with-inequality-not-equals I recommend removing that question for your post, and limiting yourself to one question per post going forward. – Frank van Puffelen Feb 12 '19 at 16:00
  • 1
    "Where there are more than two documents with the present conditions" What result are you looking to get? Note that it is often a lot easier to understand your intent when you show (pseudo) code of what you've already tried, or are trying to make work. – Frank van Puffelen Feb 12 '19 at 16:01
  • @FrankvanPuffelen Thanks for the answer. I am expecting to have a listener which can let me know if ithere is no document matching and if there are more than two results then I will want to send back a particular result via my API. – Rajesh K Feb 12 '19 at 16:11
  • @FrankvanPuffelen The link given by you does not answer my question as I want to have a listener. Will surely keep in mind your advice henceforth. – Rajesh K Feb 12 '19 at 16:16
  • The link shows: 1) that there is no "<>" condition in Firestore, 2) that you can mimic this condition by doing multiple queries. Without seeing what you've tried already to that effect and why it didn't work, it's unlikely you'll get a better answer. – Frank van Puffelen Feb 12 '19 at 16:18
  • @FrankvanPuffelen Sorry but I am not in search of "<>" queries. I want to have a simple "==" queries but I don't know the code which could be used to handle the results of the queries. My two cases state that 1) If there are no matching documents then what code should be used to detect that and 2) If the Firestore return more than one document then which code could should be used to detect that? – Rajesh K Feb 12 '19 at 16:23
  • @RajeshK If I may, from a reader perspective it is not crystal clear to understand what you exactly want to achieve :-) If you want to know if there are 0 or 2 documents that correspond to your query, you need to execute it and count the number of docs that are returned. – Renaud Tarnec Feb 12 '19 at 16:45
  • @RenaudTarnec Thanks for the comment. Can you please provide an sample code which could do that for me? Also if there is one document then how to access its data? Please Help – Rajesh K Feb 12 '19 at 16:47
  • Possible duplicate of [How to get count of documents in a collection in firestore](https://stackoverflow.com/questions/47768208/how-to-get-count-of-documents-in-a-collection-in-firestore). Just be aware that this will cost a read for each document in the collection. See also: https://stackoverflow.com/questions/48534676/how-to-count-the-number-of-documents-under-a-collection-in-firestore – Renaud Tarnec Feb 12 '19 at 17:00
  • @RenaudTarnec Both the links shared by you discuss about java. Where as I am using Node.js can you please help with this? – Rajesh K Feb 12 '19 at 17:05

2 Answers2

2

Following our "discussion" in the comments above, in a Cloud Function you could do as follows, using the QuerySnapshot returned by the get() method:

admin.firestore().collection("cities")
    .where('state', '==', 'CA')
    .get()
    .then(querySnapshot => {
        if (querySnapshot.size == 0) {
            console.log("0 documents");
        } else if (querySnapshot.size > 2) {
            console.log("More than 2 documents");
        } 
    });

As said, above, just be aware that this will cost a read for each document in the collection. In case you have a very large collection, you could write a Cloud Function that update a counter each time a doc is added/removed to/from the collection.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
1

The accepted answer does not show how to extract the data from each document and imo is only half the answer. the following will get you iterating through every document and extracting the data.

db.collection("cities").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        // doc.data() is never undefined for query doc snapshots
        console.log(doc.id, " => ", doc.data());
    });
});
basickarl
  • 37,187
  • 64
  • 214
  • 335