Skip to content

Instantly share code, notes, and snippets.

@nwatab
Created November 19, 2020 06:26
Show Gist options
  • Save nwatab/44fab6efc9c71d0a5f6eb9ef79f3cf2c to your computer and use it in GitHub Desktop.
Save nwatab/44fab6efc9c71d0a5f6eb9ef79f3cf2c to your computer and use it in GitHub Desktop.
save firestore collections
import os
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate('../your_project_credential.json')
firebase_admin.initialize_app(cred, {
'projectId': 'your_project_id'
})
db = firestore.client()
data = [{'uid': doc.id, 'name': doc.to_dict()['name']} for doc in db.collection('your_collection').stream()]
data = sorted(data, key=lambda x: x['name'], ensure_ascii=False) # for multibyte
with open('data.json', 'w') as f:
f.write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment