How to get the data from x document and upload it to a new document called ‘name’ and then delete the old one in java
JavaScript
x
const firestore = firebase.firestore();
// get the data from 'name@xxx.com'
firestore.collection("users").doc("name@xxx.com").get().then(function (doc) {
if (doc && doc.exists) {
var data = doc.data();
// saves the data to 'name'
firestore.collection("users").doc("name").set(data).then({
// deletes the old document
firestore.collection("users").doc("name@xxx.com").delete();
});
}
});
I’ve got this code snippet from a Bjorn Reemer but I am unable to get it working in android java
Advertisement
Answer
you should use a transaction
JavaScript
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference ref = db.document("users/name@xxx.com");
DocumentReference destRef = db.document("users/name");
db.runTransaction((Transaction.Function<Void>) transaction -> {
DocumentSnapshot document = transaction.get(ref);
if (document.exists()) {
transaction.set(destRef, document.getData());
transaction.delete(ref);
}
return null;
});