How to move a document in Cloud Firestore?Move a document field data in a collection to another collection firebaseCan I change the name of a document in Firestore?How to workaround firestore basic query in my scenarioRenaming document to represent logical deleteIs it possible to move Field value to another collection of same document in firebase? React NativeHow to move a subcollection on CloudFirestoreHow to exclude items from recyclerview which are existing in some Firestore CollectionIterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loopHow do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?Ship an application with a databaseHow do I obtain crash-data from my Android application?Why is the Android emulator so slow? How can we speed up the Android emulator?Is quitting an application frowned upon?How do I convert a String to an int in Java?How do I fix android.os.NetworkOnMainThreadException?How to avoid reverse engineering of an APK file?
What happens when the drag force exceeds the weight of an object falling into earth?
Why does this pattern in powers happen?
Can I bring back Planetary Romance as a genre?
What dice to use in a game that revolves around triangles?
What computer port is this?
How can it be that ssh somename works, while nslookup somename does not?
Equivalent forms of the P vs. NP problem
What are my options legally if NYC company is not paying salary?
Identity of a supposed anonymous referee revealed through "Description" of the report
Opposite party turned away from voting when ballot is all opposing party
When was it publicly revealed that a KH-11 spy satellite took pictures of the first Shuttle flight?
Capturing the entire webpage with WebExecute's CaptureImage
why it is 2>&1 and not 2>>&1 to append to a log file
And now you see it II (the B side)
What are these pads?
Magical Modulo Squares
How to start your Starctaft II games vs AI immediatly?
My Sixteen Friendly Students
Employee is self-centered and affects the team negatively
Do oversize pulley wheels increase derailleur capacity?
The unknown and unexplained in science fiction
Linear Independence for Vectors of Cosine Values
Program for finding longest run of zeros from a list of 100 random integers which are either 0 or 1
Why doesn't a particle exert force on itself?
How to move a document in Cloud Firestore?
Move a document field data in a collection to another collection firebaseCan I change the name of a document in Firestore?How to workaround firestore basic query in my scenarioRenaming document to represent logical deleteIs it possible to move Field value to another collection of same document in firebase? React NativeHow to move a subcollection on CloudFirestoreHow to exclude items from recyclerview which are existing in some Firestore CollectionIterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loopHow do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?Ship an application with a databaseHow do I obtain crash-data from my Android application?Why is the Android emulator so slow? How can we speed up the Android emulator?Is quitting an application frowned upon?How do I convert a String to an int in Java?How do I fix android.os.NetworkOnMainThreadException?How to avoid reverse engineering of an APK file?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Can someone help me how to rename, move or update document or collection names in Cloud Firestore?
Also is there anyway that I can access my Cloud Firestore to update my collections or documents from terminal or any application?
java android firebase google-cloud-firestore
add a comment |
Can someone help me how to rename, move or update document or collection names in Cloud Firestore?
Also is there anyway that I can access my Cloud Firestore to update my collections or documents from terminal or any application?
java android firebase google-cloud-firestore
add a comment |
Can someone help me how to rename, move or update document or collection names in Cloud Firestore?
Also is there anyway that I can access my Cloud Firestore to update my collections or documents from terminal or any application?
java android firebase google-cloud-firestore
Can someone help me how to rename, move or update document or collection names in Cloud Firestore?
Also is there anyway that I can access my Cloud Firestore to update my collections or documents from terminal or any application?
java android firebase google-cloud-firestore
java android firebase google-cloud-firestore
edited Feb 1 at 9:03
Alex Mamo
48.8k82967
48.8k82967
asked Nov 12 '17 at 0:51
user8748695
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Actually there is no move
method that allows you to simply move a document from a location to another. You need to create one. For moving a document from a location to another, i suugest you using the following method:
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)
Log.d(TAG, "DocumentSnapshot successfully written!");
fromPath.delete()
.addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)
Log.d(TAG, "DocumentSnapshot successfully deleted!");
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.w(TAG, "Error deleting document", e);
);
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.w(TAG, "Error writing document", e);
);
else
Log.d(TAG, "No such document");
else
Log.d(TAG, "get failed with ", task.getException());
);
In which fromPath
is the location of the document that you want to be moved and toPath
is the loaction in which you want to move the document.
The flow is as follows:
Get
the document fromfromPath
location.
Write
the document totoPath
location.
Delete
the document fromfromPath
location.
That's it!
Thanks for your help. Can you please explain how to move collection?
– user8748695
Nov 19 '17 at 11:26
In the same way as documents.fromPath
instead of pointing to a document location needs to point to a collection location.
– Alex Mamo
Nov 19 '17 at 12:15
@AlexMamo Would the above work if the document has subcollection?
– Snake
Mar 10 '18 at 5:56
2
@Snake It will move only the documents. It won't move the subcollections if the document has subcollections.
– Alex Mamo
Mar 10 '18 at 11:59
Thank you Alex. Makes sense based on your other post
– Snake
Mar 10 '18 at 19:49
add a comment |
Here's another variation for getting a collection under a new name, it includes:
- Ability to retain original ID values
- Option to update field names
$(document).ready(function ()
FirestoreAdmin.copyCollection(
'blog_posts',
'posts'
);
);
=====
var FirestoreAdmin =
// to copy changes back into original collection
// 1. comment out these fields
// 2. make the same call but flip the fromName and toName
previousFieldName: 'color',
newFieldName: 'theme_id',
copyCollection: function (fromName, toName)
FirestoreAdmin.getFromData(
fromName,
function (querySnapshot, error)
if (ObjectUtil.isDefined(error))
var toastMsg = 'Unexpected error while loading list: ' + StringUtil.toStr(error);
Toaster.top(toastMsg);
return;
var db = firebase.firestore();
querySnapshot.forEach(function (doc)
var docId = doc.id;
Logr.debug('docId: ' + docId);
var data = doc.data();
if (FirestoreAdmin.newFieldName != null)
data[FirestoreAdmin.newFieldName] = data[FirestoreAdmin.previousFieldName];
delete data[FirestoreAdmin.previousFieldName];
Logr.debug('data: ' + StringUtil.toStr(data));
FirestoreAdmin.writeToData(toName, docId, data)
);
);
,
getFromData: function (fromName, onFromDataReadyFunc)
var db = firebase.firestore();
var fromRef = db.collection(fromName);
fromRef
.get()
.then(function (querySnapshot)
onFromDataReadyFunc(querySnapshot);
)
.catch(function (error)
onFromDataReadyFunc(null, error);
console.log('Error getting documents: ', error);
);
,
writeToData: function (toName, docId, data)
var db = firebase.firestore();
var toRef = db.collection(toName);
toRef
.doc(docId)
.set(data)
.then(function ()
console.log('Document set success');
)
.catch(function (error)
console.error('Error adding document: ', error);
);
=====
Here's the previous answer where the items are added under new IDs
toRef
.add(doc.data())
.then(function (docRef)
console.log('Document written with ID: ', docRef.id);
)
.catch(function (error)
console.error('Error adding document: ', error);
);
Since the OP's question has the android tag, looks like your answer does not answer his/her question.
– Edric
Dec 26 '18 at 8:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47244403%2fhow-to-move-a-document-in-cloud-firestore%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Actually there is no move
method that allows you to simply move a document from a location to another. You need to create one. For moving a document from a location to another, i suugest you using the following method:
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)
Log.d(TAG, "DocumentSnapshot successfully written!");
fromPath.delete()
.addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)
Log.d(TAG, "DocumentSnapshot successfully deleted!");
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.w(TAG, "Error deleting document", e);
);
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.w(TAG, "Error writing document", e);
);
else
Log.d(TAG, "No such document");
else
Log.d(TAG, "get failed with ", task.getException());
);
In which fromPath
is the location of the document that you want to be moved and toPath
is the loaction in which you want to move the document.
The flow is as follows:
Get
the document fromfromPath
location.
Write
the document totoPath
location.
Delete
the document fromfromPath
location.
That's it!
Thanks for your help. Can you please explain how to move collection?
– user8748695
Nov 19 '17 at 11:26
In the same way as documents.fromPath
instead of pointing to a document location needs to point to a collection location.
– Alex Mamo
Nov 19 '17 at 12:15
@AlexMamo Would the above work if the document has subcollection?
– Snake
Mar 10 '18 at 5:56
2
@Snake It will move only the documents. It won't move the subcollections if the document has subcollections.
– Alex Mamo
Mar 10 '18 at 11:59
Thank you Alex. Makes sense based on your other post
– Snake
Mar 10 '18 at 19:49
add a comment |
Actually there is no move
method that allows you to simply move a document from a location to another. You need to create one. For moving a document from a location to another, i suugest you using the following method:
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)
Log.d(TAG, "DocumentSnapshot successfully written!");
fromPath.delete()
.addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)
Log.d(TAG, "DocumentSnapshot successfully deleted!");
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.w(TAG, "Error deleting document", e);
);
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.w(TAG, "Error writing document", e);
);
else
Log.d(TAG, "No such document");
else
Log.d(TAG, "get failed with ", task.getException());
);
In which fromPath
is the location of the document that you want to be moved and toPath
is the loaction in which you want to move the document.
The flow is as follows:
Get
the document fromfromPath
location.
Write
the document totoPath
location.
Delete
the document fromfromPath
location.
That's it!
Thanks for your help. Can you please explain how to move collection?
– user8748695
Nov 19 '17 at 11:26
In the same way as documents.fromPath
instead of pointing to a document location needs to point to a collection location.
– Alex Mamo
Nov 19 '17 at 12:15
@AlexMamo Would the above work if the document has subcollection?
– Snake
Mar 10 '18 at 5:56
2
@Snake It will move only the documents. It won't move the subcollections if the document has subcollections.
– Alex Mamo
Mar 10 '18 at 11:59
Thank you Alex. Makes sense based on your other post
– Snake
Mar 10 '18 at 19:49
add a comment |
Actually there is no move
method that allows you to simply move a document from a location to another. You need to create one. For moving a document from a location to another, i suugest you using the following method:
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)
Log.d(TAG, "DocumentSnapshot successfully written!");
fromPath.delete()
.addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)
Log.d(TAG, "DocumentSnapshot successfully deleted!");
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.w(TAG, "Error deleting document", e);
);
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.w(TAG, "Error writing document", e);
);
else
Log.d(TAG, "No such document");
else
Log.d(TAG, "get failed with ", task.getException());
);
In which fromPath
is the location of the document that you want to be moved and toPath
is the loaction in which you want to move the document.
The flow is as follows:
Get
the document fromfromPath
location.
Write
the document totoPath
location.
Delete
the document fromfromPath
location.
That's it!
Actually there is no move
method that allows you to simply move a document from a location to another. You need to create one. For moving a document from a location to another, i suugest you using the following method:
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)
Log.d(TAG, "DocumentSnapshot successfully written!");
fromPath.delete()
.addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)
Log.d(TAG, "DocumentSnapshot successfully deleted!");
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.w(TAG, "Error deleting document", e);
);
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.w(TAG, "Error writing document", e);
);
else
Log.d(TAG, "No such document");
else
Log.d(TAG, "get failed with ", task.getException());
);
In which fromPath
is the location of the document that you want to be moved and toPath
is the loaction in which you want to move the document.
The flow is as follows:
Get
the document fromfromPath
location.
Write
the document totoPath
location.
Delete
the document fromfromPath
location.
That's it!
edited Nov 13 '17 at 11:32
answered Nov 13 '17 at 11:26
Alex MamoAlex Mamo
48.8k82967
48.8k82967
Thanks for your help. Can you please explain how to move collection?
– user8748695
Nov 19 '17 at 11:26
In the same way as documents.fromPath
instead of pointing to a document location needs to point to a collection location.
– Alex Mamo
Nov 19 '17 at 12:15
@AlexMamo Would the above work if the document has subcollection?
– Snake
Mar 10 '18 at 5:56
2
@Snake It will move only the documents. It won't move the subcollections if the document has subcollections.
– Alex Mamo
Mar 10 '18 at 11:59
Thank you Alex. Makes sense based on your other post
– Snake
Mar 10 '18 at 19:49
add a comment |
Thanks for your help. Can you please explain how to move collection?
– user8748695
Nov 19 '17 at 11:26
In the same way as documents.fromPath
instead of pointing to a document location needs to point to a collection location.
– Alex Mamo
Nov 19 '17 at 12:15
@AlexMamo Would the above work if the document has subcollection?
– Snake
Mar 10 '18 at 5:56
2
@Snake It will move only the documents. It won't move the subcollections if the document has subcollections.
– Alex Mamo
Mar 10 '18 at 11:59
Thank you Alex. Makes sense based on your other post
– Snake
Mar 10 '18 at 19:49
Thanks for your help. Can you please explain how to move collection?
– user8748695
Nov 19 '17 at 11:26
Thanks for your help. Can you please explain how to move collection?
– user8748695
Nov 19 '17 at 11:26
In the same way as documents.
fromPath
instead of pointing to a document location needs to point to a collection location.– Alex Mamo
Nov 19 '17 at 12:15
In the same way as documents.
fromPath
instead of pointing to a document location needs to point to a collection location.– Alex Mamo
Nov 19 '17 at 12:15
@AlexMamo Would the above work if the document has subcollection?
– Snake
Mar 10 '18 at 5:56
@AlexMamo Would the above work if the document has subcollection?
– Snake
Mar 10 '18 at 5:56
2
2
@Snake It will move only the documents. It won't move the subcollections if the document has subcollections.
– Alex Mamo
Mar 10 '18 at 11:59
@Snake It will move only the documents. It won't move the subcollections if the document has subcollections.
– Alex Mamo
Mar 10 '18 at 11:59
Thank you Alex. Makes sense based on your other post
– Snake
Mar 10 '18 at 19:49
Thank you Alex. Makes sense based on your other post
– Snake
Mar 10 '18 at 19:49
add a comment |
Here's another variation for getting a collection under a new name, it includes:
- Ability to retain original ID values
- Option to update field names
$(document).ready(function ()
FirestoreAdmin.copyCollection(
'blog_posts',
'posts'
);
);
=====
var FirestoreAdmin =
// to copy changes back into original collection
// 1. comment out these fields
// 2. make the same call but flip the fromName and toName
previousFieldName: 'color',
newFieldName: 'theme_id',
copyCollection: function (fromName, toName)
FirestoreAdmin.getFromData(
fromName,
function (querySnapshot, error)
if (ObjectUtil.isDefined(error))
var toastMsg = 'Unexpected error while loading list: ' + StringUtil.toStr(error);
Toaster.top(toastMsg);
return;
var db = firebase.firestore();
querySnapshot.forEach(function (doc)
var docId = doc.id;
Logr.debug('docId: ' + docId);
var data = doc.data();
if (FirestoreAdmin.newFieldName != null)
data[FirestoreAdmin.newFieldName] = data[FirestoreAdmin.previousFieldName];
delete data[FirestoreAdmin.previousFieldName];
Logr.debug('data: ' + StringUtil.toStr(data));
FirestoreAdmin.writeToData(toName, docId, data)
);
);
,
getFromData: function (fromName, onFromDataReadyFunc)
var db = firebase.firestore();
var fromRef = db.collection(fromName);
fromRef
.get()
.then(function (querySnapshot)
onFromDataReadyFunc(querySnapshot);
)
.catch(function (error)
onFromDataReadyFunc(null, error);
console.log('Error getting documents: ', error);
);
,
writeToData: function (toName, docId, data)
var db = firebase.firestore();
var toRef = db.collection(toName);
toRef
.doc(docId)
.set(data)
.then(function ()
console.log('Document set success');
)
.catch(function (error)
console.error('Error adding document: ', error);
);
=====
Here's the previous answer where the items are added under new IDs
toRef
.add(doc.data())
.then(function (docRef)
console.log('Document written with ID: ', docRef.id);
)
.catch(function (error)
console.error('Error adding document: ', error);
);
Since the OP's question has the android tag, looks like your answer does not answer his/her question.
– Edric
Dec 26 '18 at 8:18
add a comment |
Here's another variation for getting a collection under a new name, it includes:
- Ability to retain original ID values
- Option to update field names
$(document).ready(function ()
FirestoreAdmin.copyCollection(
'blog_posts',
'posts'
);
);
=====
var FirestoreAdmin =
// to copy changes back into original collection
// 1. comment out these fields
// 2. make the same call but flip the fromName and toName
previousFieldName: 'color',
newFieldName: 'theme_id',
copyCollection: function (fromName, toName)
FirestoreAdmin.getFromData(
fromName,
function (querySnapshot, error)
if (ObjectUtil.isDefined(error))
var toastMsg = 'Unexpected error while loading list: ' + StringUtil.toStr(error);
Toaster.top(toastMsg);
return;
var db = firebase.firestore();
querySnapshot.forEach(function (doc)
var docId = doc.id;
Logr.debug('docId: ' + docId);
var data = doc.data();
if (FirestoreAdmin.newFieldName != null)
data[FirestoreAdmin.newFieldName] = data[FirestoreAdmin.previousFieldName];
delete data[FirestoreAdmin.previousFieldName];
Logr.debug('data: ' + StringUtil.toStr(data));
FirestoreAdmin.writeToData(toName, docId, data)
);
);
,
getFromData: function (fromName, onFromDataReadyFunc)
var db = firebase.firestore();
var fromRef = db.collection(fromName);
fromRef
.get()
.then(function (querySnapshot)
onFromDataReadyFunc(querySnapshot);
)
.catch(function (error)
onFromDataReadyFunc(null, error);
console.log('Error getting documents: ', error);
);
,
writeToData: function (toName, docId, data)
var db = firebase.firestore();
var toRef = db.collection(toName);
toRef
.doc(docId)
.set(data)
.then(function ()
console.log('Document set success');
)
.catch(function (error)
console.error('Error adding document: ', error);
);
=====
Here's the previous answer where the items are added under new IDs
toRef
.add(doc.data())
.then(function (docRef)
console.log('Document written with ID: ', docRef.id);
)
.catch(function (error)
console.error('Error adding document: ', error);
);
Since the OP's question has the android tag, looks like your answer does not answer his/her question.
– Edric
Dec 26 '18 at 8:18
add a comment |
Here's another variation for getting a collection under a new name, it includes:
- Ability to retain original ID values
- Option to update field names
$(document).ready(function ()
FirestoreAdmin.copyCollection(
'blog_posts',
'posts'
);
);
=====
var FirestoreAdmin =
// to copy changes back into original collection
// 1. comment out these fields
// 2. make the same call but flip the fromName and toName
previousFieldName: 'color',
newFieldName: 'theme_id',
copyCollection: function (fromName, toName)
FirestoreAdmin.getFromData(
fromName,
function (querySnapshot, error)
if (ObjectUtil.isDefined(error))
var toastMsg = 'Unexpected error while loading list: ' + StringUtil.toStr(error);
Toaster.top(toastMsg);
return;
var db = firebase.firestore();
querySnapshot.forEach(function (doc)
var docId = doc.id;
Logr.debug('docId: ' + docId);
var data = doc.data();
if (FirestoreAdmin.newFieldName != null)
data[FirestoreAdmin.newFieldName] = data[FirestoreAdmin.previousFieldName];
delete data[FirestoreAdmin.previousFieldName];
Logr.debug('data: ' + StringUtil.toStr(data));
FirestoreAdmin.writeToData(toName, docId, data)
);
);
,
getFromData: function (fromName, onFromDataReadyFunc)
var db = firebase.firestore();
var fromRef = db.collection(fromName);
fromRef
.get()
.then(function (querySnapshot)
onFromDataReadyFunc(querySnapshot);
)
.catch(function (error)
onFromDataReadyFunc(null, error);
console.log('Error getting documents: ', error);
);
,
writeToData: function (toName, docId, data)
var db = firebase.firestore();
var toRef = db.collection(toName);
toRef
.doc(docId)
.set(data)
.then(function ()
console.log('Document set success');
)
.catch(function (error)
console.error('Error adding document: ', error);
);
=====
Here's the previous answer where the items are added under new IDs
toRef
.add(doc.data())
.then(function (docRef)
console.log('Document written with ID: ', docRef.id);
)
.catch(function (error)
console.error('Error adding document: ', error);
);
Here's another variation for getting a collection under a new name, it includes:
- Ability to retain original ID values
- Option to update field names
$(document).ready(function ()
FirestoreAdmin.copyCollection(
'blog_posts',
'posts'
);
);
=====
var FirestoreAdmin =
// to copy changes back into original collection
// 1. comment out these fields
// 2. make the same call but flip the fromName and toName
previousFieldName: 'color',
newFieldName: 'theme_id',
copyCollection: function (fromName, toName)
FirestoreAdmin.getFromData(
fromName,
function (querySnapshot, error)
if (ObjectUtil.isDefined(error))
var toastMsg = 'Unexpected error while loading list: ' + StringUtil.toStr(error);
Toaster.top(toastMsg);
return;
var db = firebase.firestore();
querySnapshot.forEach(function (doc)
var docId = doc.id;
Logr.debug('docId: ' + docId);
var data = doc.data();
if (FirestoreAdmin.newFieldName != null)
data[FirestoreAdmin.newFieldName] = data[FirestoreAdmin.previousFieldName];
delete data[FirestoreAdmin.previousFieldName];
Logr.debug('data: ' + StringUtil.toStr(data));
FirestoreAdmin.writeToData(toName, docId, data)
);
);
,
getFromData: function (fromName, onFromDataReadyFunc)
var db = firebase.firestore();
var fromRef = db.collection(fromName);
fromRef
.get()
.then(function (querySnapshot)
onFromDataReadyFunc(querySnapshot);
)
.catch(function (error)
onFromDataReadyFunc(null, error);
console.log('Error getting documents: ', error);
);
,
writeToData: function (toName, docId, data)
var db = firebase.firestore();
var toRef = db.collection(toName);
toRef
.doc(docId)
.set(data)
.then(function ()
console.log('Document set success');
)
.catch(function (error)
console.error('Error adding document: ', error);
);
=====
Here's the previous answer where the items are added under new IDs
toRef
.add(doc.data())
.then(function (docRef)
console.log('Document written with ID: ', docRef.id);
)
.catch(function (error)
console.error('Error adding document: ', error);
);
edited Apr 16 '18 at 22:18
answered Apr 2 '18 at 20:10
Gene BoGene Bo
5,76044396
5,76044396
Since the OP's question has the android tag, looks like your answer does not answer his/her question.
– Edric
Dec 26 '18 at 8:18
add a comment |
Since the OP's question has the android tag, looks like your answer does not answer his/her question.
– Edric
Dec 26 '18 at 8:18
Since the OP's question has the android tag, looks like your answer does not answer his/her question.
– Edric
Dec 26 '18 at 8:18
Since the OP's question has the android tag, looks like your answer does not answer his/her question.
– Edric
Dec 26 '18 at 8:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47244403%2fhow-to-move-a-document-in-cloud-firestore%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown