I'm trying to save to document ID inside a field in the same document. I'm currently doing it like this:
const myDoc = await firestore().collection("users").doc(auth().currentUser?.uid).collection("myCol").add({
test: "Hello, World!"
});
myDoc.update({id: myDoc.id})
Is it possible to do this without having to write to the database 2 times?
This is stated in this documentation, for reference, see snippet below:
const myDoc = firestore().collection("users").doc(auth().currentUser?.uid).collection("myCol").doc();
await myDoc.set({
test: "Hello, World!",
id: myDoc.id
});
This would get the document ID that you would use when adding a document. You need to use the set()
method instead of add()
method.
You could also use uuid to set a custom document ID when adding it in your Firestore Database.