firebase firestore add doc with random id code example
Example 1: firestore create document with auto id
// Add a new document with a generated id.
db.collection("cities").add({
name: "Tokyo",
country: "Japan"
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
---------------------------
//If you want to specify id then use .set()
// e.x \/
db.collection("cities").doc("TYO").set({
name: "Tokyo",
country: "Japan"
})
Example 2: document.set() firebasefirestore java
// Create an initial document to update.var frankDocRef = db.collection("users").doc("frank");frankDocRef.set({ name: "Frank", favorites: { food: "Pizza", color: "Blue", subject: "recess" }, age: 12});// To update age and favorite color:db.collection("users").doc("frank").update({ "age": 13, "favorites.color": "Red"});