Is this possible to move a document from one collection to another in Firestore Database? The reason I want to do this is, when someone deletes an Order, I want to move it to deleted_order
s collection rather than deleting the entire document to keep the history.
I want to do this from my app which I have developed in Kotlin
.
Asked
Active
Viewed 1,673 times
0
-
1In code, it should look like [this](https://stackoverflow.com/questions/47244403/how-to-move-a-document-in-cloud-firestore). – Alex Mamo Oct 16 '21 at 09:11
2 Answers
4
The only way to move a document from one collection to another collection is to use the following sequence, mimicking a "cut & paste":
- Create a copy of the source document in the target collection, i.e. read it from the source collection, get the document fields and create a new document in the target collection with these fields, and then;
- Delete the document from the source collection.
In other words, there is no kind of "metadata" attached to a document that defines its parent collection and that you could change in one simple action.
Another approach would be to have all the order
docs in a unique collection and a field which holds the order status, e.g. active
or deleted
. Then changing the order status is just a matter of updating a field.
Note that this approach will not have any negative effect on your queries performance since Firestore query performance is proportional to the size of the result set, not the data set size.

Renaud Tarnec
- 79,263
- 10
- 95
- 121
-
Thanks for your response, I had already thought about the second method (Changing the status to 'Deleted') which you have mentioned but I thought that may not be a good idea, professional way of doing it, also I wasn't sure how it will affect my queries, whether this will cause to increase my Reads count whenever I want to get the list of orders. – Codist Oct 16 '21 at 09:55
-
As explained at the end of the answer, it will not affect your queries performance. It is not a problem at all to put everything in one collection. Just take care to correctly set the corresponding security rules, for example to forbid users to read deleted orders based on the status field and not anymore on the collection path. – Renaud Tarnec Oct 16 '21 at 10:10
-
1PS: "whether this will cause to increase my Reads count" => Just filter your queries on the status_field and you are only going to pay for the desired docs (e.g. only the deleted orders or only the active orders). **You only pay for the documents returned by the query** (Note that "there is a minimum charge of one document read for each query that you perform, even if the query returns no results") See https://firebase.google.com/docs/firestore/pricing#operations – Renaud Tarnec Oct 16 '21 at 10:36
-1
private void insertAllFromOneCollectionIntoAnother(CollectionReference crFrom, CollectionReference crTo) {
QuerySnapshot querySnapshot = crFrom.GetSnapshotAsync().Result;
foreach (DocumentSnapshot documentSnapshot in querySnapshot.Documents) {
crTo.Document(documentSnapshot.Id.ToString())
.SetAsync(documentSnapshot.ToDictionary());
}
}

Kevin Kozakewich
- 1
- 3