Reputation:
I need to add nested json data to an existing document in firestore. I have tried one of the codes found online using nodeJs, but it replaced the document I had before. Can any one suggest a code snippet either in python or nodeJs or html? My document looks like this-
Have tried the below code in nodejs
data && Object.keys(data).forEach(key => {
const nestedContent = data[key];
if (typeof nestedContent === "object") {
Object.keys(nestedContent).forEach(docTitle => {
admin.firestore()
.collection(key)
.doc(docTitle)
.set(nestedContent[docTitle])
.then((res) => {
console.log("Document successfully written!");
})
.catch((error) => {
console.error("Error writing document: ", error);
});
});
}
Upvotes: 0
Views: 1322
Reputation: 479
Without watching your code it's hard to say but something like this should work
firebase.firestore().collection("parentCollection").doc("idOfTheItemToMutate").collection("nestedCollection").add({
key1: value1,
key2: value2
})
Upvotes: 1