javascriptfirebasegoogle-cloud-firestore

firebase firestore querying based on a property in a map


i have data structured in firestore like this

users (collection)
  --> uid (document)
      --> profile (map field)
         --> status (field)

I want to query the data that matches to a value of status "Active" and wrote like below:

 const appColRef = db.collection("users"); 
 const querySnapshot = await appColRef.where("status", '=', 'Active').get()

above does not work and would work if status is at the same level at profile. so is there a way to query the status field inside profile map field.


Solution

  • If you want to query on a property in a map field, you can use dot notation like this:

    appColRef.where("profile.status", '=', 'Active')
    

    You will need to ensure that you have the necessary index for the query though, so be sure to check any error message you get back for information about missing indexes.

    Some examples that cover this: