Search code examples
firebasegoogle-cloud-firestore

firebase firestore querying based on nested attribute


Here is how the data is structured in my firebase firestore

users: {
xyz: {
 profile: {
    email: "[email protected]"
}
},
pqr:  {
profile: {
email: "[email protected]"
}
}

but as email is optional in my system so i want get only the users that have a not null email and a non blank email. How do i query those?

As a context, I am coming from realtime database World where i was forced to store the email address directly under user id node rather a level down. Do i have to do the same thing in firestore as well?


Solution

  • As mentioned in the firestore documentation:

    A field exists when it's set to any value, including an empty string (""), null, and NaN (not a number). Note that null field values do not match != clauses, because x != null evaluates to undefined.

    fireClient.Collection("users").Where("profile.email", "!=", "").Documents(ctx).GetAll() //golang code reference
    

    This query should return every user document where the email field exists with a value other than "" or null

    Reference: Cloud firestore documentation