I need to transfer data from a document belonging to one collection to another. It doesn't matter how to do this (rename the sub-collection in the document, or copy the data and then delete it).
I know about one example, but I need a detailed analysis of it, preferably with an example.
The same example:
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
moveFirestoreDocument();
}
});
}
public void moveFirestoreDocument(DocumentReference fromPath, final DocumentReference toPath) {
fromPath.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
toPath.set(document.getData())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
fromPath.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
} else {
}
} else {
}
}
});
}
Code with moving a specific record:
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CollectionReference addToCartRef1 = firestore.collection("AddToCart");
DocumentReference addToCartRef2 = addToCartRef1.document(auth.getCurrentUser().getUid());
CollectionReference addToCartRef3 = addToCartRef2.collection("Product");
DocumentReference fromPath = addToCartRef3.document("DiaSef1RX9xlAFm26P8M");
CollectionReference deletedAddToCartRef1 = firestore.collection("AddToPurchased");
DocumentReference deletedAddToCartRef2 = deletedAddToCartRef1.document(auth.getCurrentUser().getUid());
CollectionReference deletedAddToCartRef3 = deletedAddToCartRef2.collection("Product");
DocumentReference toPath = deletedAddToCartRef3.document("DiaSef1RX9xlAFm26P8M");
moveFirestoreDocument(fromPath, toPath);
}
});
Continuation of the database structure
CodePudding user response:
How exactly to assign collections for "fromPath" and "toPath"?
The "fromPath" and "toPath" are not collection references, but document references. Since the moveFirestoreDocument()
method contains two arguments, you have to call the method and pass two document references as arguments. So assuming you have two collections that look like this:
Firestore-root
|
--- products (collection)
| |
| --- $productId (document)
| |
| --- //Document details.
|
--- deletedProducts (collection)
|
--- $productId (document)
|
--- //Document details.
To move a document from one collection to the other, please use the following lines of code:
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference productsRef = db.collection("products");
DocumentReference fromPath = productsRef.document(productId);
CollectionReference deletedProductsRef = db.collection("deletedProducts");
DocumentReference toPath = deletedProductsRef.document(productId);
moveFirestoreDocument(fromPath, toPath);