My firestore collection is structured as follows:
events/{2022}/January/{someDocumentId}
How do I only export January subcollection?
I've been trough these docs https://firebase.google.com/docs/firestore/manage-data/export-import, but can only find info on exporting entire database and "events" root collections. The reason is I want to keep my cost down.
How do I only export January subcollection?
You can filter specific collections by using the collection-id flag. Following the link you've already provided, you can follow the command that was provided:
gcloud firestore export gs://[BUCKET_NAME] --collection-ids=[COLLECTION_ID_1],[COLLECTION_ID_2]
Just a note, using collection-id flag is used to import specific collection groups from a set of export files, and if there's a collection that you don't want to export, you can exclude them from the export operation by using the flag.
Additionally, if you want to backup the whole collection and its sub collections without using the collection-id flag, you can use collection group query which retrives all the documents from a collection group instead from a single collection. There's also a related post that has a good answers on how to fecth all documents from a Firebase collection using collection group query.
Related
Is it possible to get data of sub-documents by reading only the main document in Firestore? Basically, the Firebase listCollections() method gives the id of subcollections, but how can we get their data?
I have tried for listCollections() to get the id of sub-collections, but have not found a way to get data sub-collections data.
Is it possible to get data of sub-documents by reading only the main document in Firebase?
In Firestore, the queries are shallow. This means that it can only return documents from the collection that the query is run against. So there is no way you can get documents along with the data that exists inside sub-collections in one go. A single query can only read fields of documents in a single collection.
If you need to get documents from sub-collection, you need to perform separate queries. If you have collections or sub-collections that have the exact same name then you can use a collection group query.
Firebase listCollectionIds(),Firebase listDocuments() are well documented by firebase but nobody use and write on it....
I think the best way is to list subcollection to your document throught the well-know get() and then process a foreach on result snapshot
snapshot.forEach(doc =>{
console.log(doc.id)
console.log(doc.data())
})
With gcloud, it's very straightforward to export all documents within a firestore project, or specific collections within a project. (See here for more). However, I'm trying to export solely one document from within a collection. Is that at all possible? I can't seem to find any documentation on this.
As per my investigation, the feature to export a single document within a collection is not supported. You can find the feature request in the public issue tracker. You can go ahead and subscribe/star the request for additional updates/workarounds from the product team.
A workaround that’s possible is that you can use SubCollection that comes under the root level Collection to export. You will have to make sure to name the sub collections uniquely. Firestore will export these as a flat list and two sub collections with the same id/name will be combined. To do this you can first create a subcollection under the main collection and then add the documents that needs to be exported, and use the following command to export the subcollection:
gcloud beta firestore export gs://export_123 --collection-ids = ’sub-collectionID1’ , ’sub-collectionID2’
Another possible workaround is that you can export the whole collection to cloud storage, then load the data from firestore export it to the big query , and then use jquery or any other JSON tool that you prefer to extract the document that you require.
My cloud firestore database has an "orders" collection and in HTML I have a 'save' button to add document(s) into that "orders" collection upon clicking. Now, using add will assign auto-generated ID for each document.
What if I want to customise such ID by timestamp? So that the document created yesterday will be assigned an index as '1', and the following document created will be '2', etc...
What you're trying to do is not compatible with the way Cloud Firestore was designed. Firestore will not assign monotonically increasing numbers for document IDs. This just doesn't scale massively as required by Firestore and would introduce performance bottlenecks.
If you want to be able to sort documents by timestamp, the best strategy is to add a timestamp field to each document, then use that field in an ordered query.
Note that you could try to write a lot of code to get this done the way you want, but you are MUCH better off accepting the random IDs and using fields to filter and order data.
in some case, when you need to save several docs in different collection due to an event occurs, it's better to same all docs with same id in different collections with single firestore server's timestamp. you get the timestamp like below:
const admin = require('firebase-admin')
const ts = admin.firestore.Timestamp.now().toMillis().toString()
by doing this, when you need to read all those docs, you only need to query once to get timestamp, then read all other doc by timestamp directly.
it should be faster than query the timestamp inside document fields for each collections
I accidentally made a mistake in a firestore collection name and would like to rename it but there is no option to rename a collection in firestore. There is an option to export and then import the collection to GCP using the following command:
gcloud firestore export gs://[BUCKET_NAME] --collection-ids=[COLLECTION_ID_1],[COLLECTION_ID_2]
Will this only reexport the collection with the same name. Is there any other way to do this other than writing a script or doing it manually?
If you want to change the names and IDs of collections and documents (or in other words, change the path of the document), the only way to do that is to read each document, then write it to its new location. This typically involves writing code to perform a query, iterate the results, write new documents, and delete the originals.
Unless you are able to find a library to do all this for you, you'll have to write the code yourself.
I am working on angular 5 with firebase firestore.I inserted data into fire-store collection/document/collection/document/collection/document then set data the last document.But at the time retrieving data I am giving only first collection name then I want complete root data.but I am getting only field in it.
https://firebase.google.com/docs/firestore/query-data/get-data
checkout:List subcollections of a document
As we know querying in Cloud Firestore is shallow by default. This type of query isn't supported, although it is something Google may consider in the future.