Ad

Firestore: How To Query A Map Object Inside A Collection Of Documents?

My objective:

The web app (similar to Google Drive) has 2 screens.

First screen is 'My Story' which displays all the story documents whom I'm the owner.

Second screen is 'Shared with me' which displays all the story documents that I'm either reader, writer, or commenter.

We have this /stories/{storyid}

{
  title: "A Great Story",
  content: "Once upon a time ...",
  roles: {
    alice: "owner",
    bob: "reader",
    david: "writer",
    jane: "commenter"
    // ...
  }
}

Full data structure reference: https://firebase.google.com/docs/firestore/solutions/role-based-access

Question: How to write the below query to fulfil the above objective?

db
.collection('stories')
.where(`roles.${uid}`, '==', 'owner')

The above query does NOT work because it will require Firestore to index roles.alice, roles.bob, roles.david, roles.jane, and roles.[all_uid].


Edit #1: Found a related post (with no answers) asking about Firestore querying with roles

Ad

Answer

Now you can filter ...

db
  .collection('orders')
  .where('orderDetails.status', '==', 'OPEN')

It will check if field orderDetails at property status equals to 'OPEN'

Ad
source: stackoverflow.com
Ad