Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export specific Firestore subcollection

I am trying to export/import a specific sub-collection in Firestore using the beta version of the firestore export tool.

For example, if I have a setup of

  • companies
  • companies/{company}/users
  • teams
  • teams/{team}/users

I want to just get the companies collection and all of its subcollections, I was looking to do:

gcloud beta firestore export gs://my-bucket-name/fs_backups/companies --collection-ids='companies','users'

And I would get companies, companies/{company}/users and teams/{company}/users.

I was trying all of these combos:

... -collection-ids='companies/{id}/users'
... -collection-ids='companies/./users'
... -collection-ids='companies/.*/users'
... -collection-ids='companies/*/users'
... -collection-ids='companies/**/users'
... -collection-ids='companies/__name__/users'

and some other ones as well, but was getting empty (but not failing) results.

Is there a notation for the collection ids to specify the subcollection that I am looking to export?

like image 856
A Sears Avatar asked Dec 04 '18 17:12

A Sears


People also ask

How do I get sub collection in firestore?

firestore(). collection("reservations/" + tripUid + "/ymuIjdvwWnOr20XWVp6gwRKtmgD2"). get() . This will get all the sub collection documents under the reservation with an held within tripUid variable and within the collection 'ymuIjdvwWnOr20XWVp6gwRKtmgD2'.

How do I export JSON from firestore?

Export Firestore Query Results to JSONClick the collection in the sidebar on the left. Add Where conditions and Order by clauses. Adjust the number of documents with the Limit field, click the Run button to run your query! Click the Export Results icon as seen in the screenshot below.

What is a Subcollection in firestore?

A subcollection is a collection associated with a specific document. Note: You can query across subcollections with the same collection ID by using Collection Group Queries. You can create a subcollection called messages for every room document in your rooms collection: collections_bookmark rooms. class roomA.


1 Answers

I ran into the same problem. The issue is that at least two subcollections, in your example, have the name users. The export functionality, when selecting specific collections/subcollections, "flattens" the data (i.e. subcollections will be exported into its own folder, as opposed to a subfolder within the parent collection). This makes subcollections with the same name ambiguous. The fix is to uniquely name subcollections and collections (this will also be helpful if you plan to later import it into BigQuery).

like image 103
David Aroesti Avatar answered Oct 19 '22 05:10

David Aroesti