SpaceX
SpaceX

Reputation: 2890

How to query for objects with a certain property in a Firestore collection, Firebase

I am trying to query for objects in firestore collection with a certain property, my Firestore DB looks as shown in the below picture.

The below code does not work as I am interested in querying for objects based on the the property inside an object. In this case I want all the cases (objects) with "status"=="treated" Is there a way to do it ?

  const db = firebase.firestore();

  const casesRef = db.collection("mgm/hospital/cases");

  // Create a query against the collection.
  var query = casesRef.where("status", "==", "active").limit(25);
  const queryRef = query.get();

  queryRef.then(function(documentSnapshots) {
    const cases = documentSnapshots.docs.map((doc) => doc.data());
    console.log(cases);
  }); 

enter image description here

Upvotes: 0

Views: 966

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

Since the field patientDetails is a map, the following should do the trick:

var query = casesRef.where("patientDetails.status", "==", "active").limit(25);

Upvotes: 2

Related Questions