Gray Hat
Gray Hat

Reputation: 368

Can I query a field inside nested Document in firestore?

I have a structure like :

[Collection] users
     [Document] id
          [Collection] public
               [Document] data
                    [Field] isArtist: boolean
                    [Field] name: string
          [Collection] private
               [Document] data
                    //...some fields
     [Document] id
          [Collection] public
               [Document] data
                    [Field] isArtist: boolean
                    [Field] name: string
          [Collection] private
               [Document] data
                    //...some fields

Now I wanna query all users with

{
     isArtist : true,
     name: "something"
}

I don't really see anything helpful for this case in the documentation.

Upvotes: 1

Views: 89

Answers (1)

Brian Burton
Brian Burton

Reputation: 3842

You can do this with collection group queries. Something like this should work:

const query = db.collectionGroup('public')
  .where('name', '==', 'something')
  .where('isArtist', '==', true);

let matches = [];

query.get().then((snapshot) => {
  snapshot.forEach((doc) => {
    matches.push({id: doc.id, ...(doc.data())});
  });
});

Upvotes: 2

Related Questions