How to list subcollections in a Cloud Firestore document

后端 未结 3 1258
名媛妹妹
名媛妹妹 2020-11-22 07:59

Say I have this minimal database stored in Cloud Firestore. How could I retrieve the names of subCollection1 and subCollection2?

ro         


        
相关标签:
3条回答
  • 2020-11-22 08:11

    It seems like they have added a method called getCollections() to Node.js:

    firestore.doc(`/myCollection/myDocument`).getCollections().then(collections => {
      for (let collection of collections) {
        console.log(`Found collection with id: ${collection.id}`);
      }
    });
    

    This example prints out all subcollections of the document at /myCollection/myDocument

    0 讨论(0)
  • 2020-11-22 08:27

    Isn't this detailed in the documentation?

    /**
     * Delete a collection, in batches of batchSize. Note that this does
     * not recursively delete subcollections of documents in the collection
     */
    function deleteCollection(db, collectionRef, batchSize) {
        var query = collectionRef.orderBy('__name__').limit(batchSize);
    
        return new Promise(function(resolve, reject) {
            deleteQueryBatch(db, query, batchSize, resolve, reject);
        });
    }
    
    function deleteQueryBatch(db, query, batchSize, resolve, reject) {
        query.get()
            .then((snapshot) => {
                // When there are no documents left, we are done
                if (snapshot.size == 0) {
                    return 0;
                }
    
                // Delete documents in a batch
                var batch = db.batch();
                snapshot.docs.forEach(function(doc) {
                    batch.delete(doc.ref);
                });
    
                return batch.commit().then(function() {
                    return snapshot.size;
                });
            }).then(function(numDeleted) {
                if (numDeleted <= batchSize) {
                    resolve();
                    return;
                }
    
                // Recurse on the next process tick, to avoid
                // exploding the stack.
                process.nextTick(function() {
                    deleteQueryBatch(db, query, batchSize, resolve, reject);
                });
            })
            .catch(reject);
    }
    
    0 讨论(0)
  • 2020-11-22 08:34

    In Node.js you'll be after the 'ListCollectionIds' method

    var firestore = require('firestore.v1beta1');
    
    var client = firestore.v1beta1({
      // optional auth parameters.
    });
    
    // Iterate over all elements.
    var formattedParent = client.anyPathPath("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
    
    client.listCollectionIds({parent: formattedParent}).then(function(responses) {
        var resources = responses[0];
        for (var i = 0; i < resources.length; ++i) {
            // doThingsWith(resources[i])
        }
    })
    .catch(function(err) {
        console.error(err);
    });
    

    This is not currently supported in the client SDKs (Web, iOS, Android).

    0 讨论(0)
提交回复
热议问题