How can I query map object which is the document ID in firebase?

Viewed 2348

I am trying to fetch all fieldnames inside of payload > (random doc id) objects.

enter image description here

Currently I am fetching other collections with:

async fetchPage() {
  const query = firebase
    .firestore()
    .collection('PAGES')
    .where('type', '==', 'page')
  try {
    const { docs } = await query.get()

    this.pageIndex = docs.map((doc) => {
      if (doc && doc.exists) {
        this.items = doc.data()
      }
      let { id } = doc
      const data = doc.data()
      return { id, ...data }
    })

    console.log('Loaded items', this.items)
  } catch (error) {
    throw new Error('Something gone wrong!')
  }
},

The first question is: What is the best practice to query objects? I read this in firebase docs but didn't lead me to any results trying it out that way.

Second question, since the child object of payload is the actual id of its document, what method must I use to declare that I'm looking for e.g payload > doc id > content: "This is some content"?.

1 Answers

Here are some information that are helpful about how Firestore operates and they may prove helpful to you on how to improve queries in your database.

Here is the Firebase documentation about queries and the Query Client Library.

This is an article that refers to the causes of slow queries.

Regarding your second question you can get a snapshot of the whole document and then you have to iterate through the data.

Let me know if this is helpful to you.

Related