Reputation: 1334
I am using Python to conect to a firestore database from a client.
The problem is that I don't know how to see what collections he has in the database:
from google.cloud import firestore
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate('credentials/credentials.json')
app = firebase_admin.initialize_app(cred)
db = firestore.client()
users_ref = db.collection(u'name_of_colection')
docs = users_ref.stream()
for doc in docs:
print(u'{} => {}'.format(doc.id, doc.to_dict()))
I have been looking how to get the name of the collections that he has but I didn't find anything that it was useful to me. I have also tried this:
cols = db.collections()
list_col = []
for col in cols:
list_col.append(col)
len(list_col)
I have obtained len = 6
Then I have done this for the different col in the list I have generated:
docs = list_col[5].stream()
data = []
for doc in docs:
data.append(doc.to_dict())
print(data)
This data print a dictionary with keys and values, but I don't know only get a list with the name of collections,
Upvotes: 1
Views: 2551
Reputation: 1686
I can't believe how hard it has been to find on the internet the answer to the question How to list all collections in Firestore in Python?
. Now, consolidating the answers posted by others, here is the minimal code:
from google.cloud import firestore
db = firestore.Client()
# The following call returns a Python Generator
# https://github.com/googleapis/python-firestore/blob/532aff8d7b5bfde8b73be6b7e508f9d9fd6b5254/google/cloud/firestore_v1/client.py#L271-L297
cols = db.collections()
for c in cols:
print(c.id)
Amazingly, the seemingly extensive set of official GCP Firestore code samples (or its corresponding Github snippets) doesn't have such an example.
This really shouldn't have been such a hard-to-find answer.
Upvotes: 3
Reputation: 199
Simple solution:
import firebase_admin
from firebase_admin import firestore
app_options = {'projectId': 'test-project'}
default_app = firebase_admin.initialize_app(options=app_options)
db = firestore.client()
collection = db.collections()
list_col = []
for col in collection:
list_col.append(col.id)
print(list_col)
Upvotes: 1
Reputation: 2519
I think that you have to get the id
from each collection (which is the collection name
you are talking about
list_col = []
for col in collections:
list_col.append(col.id) // <-- add this please
print(list_col)
I hope it helps you
Upvotes: 5
Reputation: 2773
Any collection you see in fire base depends on your rights. you can use
query = client.collection_group('mygroup')
or
query = client.collections()
It gives top hierarchy and you have to run multiple times to find the lowest document level.
query = client.collection_group('mygroup')
@param {string} collectionId Identifies the collections to query over. Every collection or subcollection with this ID as the last segment of its path will be included. Cannot contain a slash. @returns {Query} The created Query.
collections()[source]
List top-level collections of the client’s database.
Returns
iterator of subcollections of the current document.
Return type
Sequence[CollectionReference]
Upvotes: 0