Is there a method to iterate through all documents in a collection in firestore

前端 未结 2 1449
小蘑菇
小蘑菇 2021-02-20 01:45

I\'m using the firestore of firebase and I want to iterate through the whole collection. Is there something like:

db.collection(\'something\').forEach((doc) =>         


        
相关标签:
2条回答
  • 2021-02-20 02:20

    Yes, you can simply query the collection for all its documents using the get() method on the collection reference. A CollectionReference object subclasses Query, so you can call Query methods on it. By itself, a collection reference is essentially an unfiltered query for all of its documents.

    Android: Query.get()

    iOS/Swift: Query.getDocuments()

    JavaScript: Query.get()

    In each platform, this method is asynchronous, so you'll have to deal with the callbacks correctly.

    See also the product documentation for "Get all documents in a collection".

    0 讨论(0)
  • 2021-02-20 02:29
    db.collection("cities").get().then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            // doc.data() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data());
        });
    });
    
    0 讨论(0)
提交回复
热议问题