Getting all documents from one collection in Firestore

176,358

Solution 1

The example in the other answer is unnecessarily complex. This would be more straightforward, if all you want to do is return the raw data objects for each document in a query or collection:

async getMarker() {
    const snapshot = await firebase.firestore().collection('events').get()
    return snapshot.docs.map(doc => doc.data());
}

Solution 2

if you want include Id

async getMarkers() {
  const events = await firebase.firestore().collection('events')
  events.get().then((querySnapshot) => {
      const tempDoc = querySnapshot.docs.map((doc) => {
        return { id: doc.id, ...doc.data() }
      })
      console.log(tempDoc)
    })
}

Same way with array

async getMarkers() {
  const events = await firebase.firestore().collection('events')
  events.get().then((querySnapshot) => {
      const tempDoc = []
      querySnapshot.forEach((doc) => {
         tempDoc.push({ id: doc.id, ...doc.data() })
      })
      console.log(tempDoc)
   })
 }

Solution 3

I made it work this way:

async getMarkers() {
  const markers = [];
  await firebase.firestore().collection('events').get()
    .then(querySnapshot => {
      querySnapshot.docs.forEach(doc => {
      markers.push(doc.data());
    });
  });
  return markers;
}

Solution 4

if you need to include the key of the document in the response, another alternative is:

async getMarker() {
    const snapshot = await firebase.firestore().collection('events').get()
    const documents = [];
    snapshot.forEach(doc => {
       const document = { [doc.id]: doc.data() };
       documents.push(document);
    }
    return documents;
}

Solution 5

You could get the whole collection as an object, rather than array like this:

async getMarker() {
    const snapshot = await firebase.firestore().collection('events').get()
    const collection = {};
    snapshot.forEach(doc => {
        collection[doc.id] = doc.data();
    });
    return collection;
}

That would give you a better representation of what's in firestore. Nothing wrong with an array, just another option.

Share:
176,358

Related videos on Youtube

Stroi
Author by

Stroi

Updated on July 08, 2022

Comments

  • Stroi
    Stroi almost 2 years

    Hi I'm starting with javascript and react-native and I'm trying to figure out this problem for hours now. Can someone explain me how to get all the documents from firestore collection ?

    I have been trying this:

    async getMarkers() {
      const events = await firebase.firestore().collection('events').get()
        .then(querySnapshot => {
          querySnapshot.docs.map(doc => {
            console.log('LOG 1', doc.data());
            return doc.data();
          });
        });
      console.log('LOG 2', events);
      return events;
    }
    

    Log 1 prints all the objects(one by one) but log 2 is undefined, why ?

  • etoxin
    etoxin almost 5 years
    this worked for me const snapshot = await firestore.collection('events').get()
  • Doug Stevenson
    Doug Stevenson almost 5 years
    @etoxin That would work only if your imports/requires were different than what the OP was doing.
  • Mentor
    Mentor almost 5 years
    Would this count towards one read for the quota, or would each document in the collection count as a read operation?
  • Doug Stevenson
    Doug Stevenson almost 5 years
    @Mentor The query will incur a read for every matched document, regardless of what you do with the snapshots in your code.
  • Oleksii.B
    Oleksii.B about 4 years
    const events = await firestore.collection('events').get().then(snapshot => snapshot.docs.map(doc => doc.data()))
  • bermick
    bermick about 4 years
    would this do one trip to db or one per each document?
  • Doug Stevenson
    Doug Stevenson about 4 years
    @bermick one query is one round trip. The entire results of the query come back in the snapshot.
  • Mister SirCode
    Mister SirCode over 3 years
    snapshot.docs no longer functions or is deprecated @DougStevenson
  • Doug Stevenson
    Doug Stevenson over 3 years
    @MisterSirCode No, docs is still a property on QuerySnapshot.
  • Mister SirCode
    Mister SirCode over 3 years
    Oh, my apologies, I was confusing Firebase Real-time Database and Firestore... turns out I was accidentally on the database documentation instead of firestore, so it's all good