firestore set data code example
Example 1: firestore set a document
let data = {
name: 'Los Angeles',
state: 'CA',
country: 'USA'
};
// Add a new document in collection "cities" with ID 'LA'
let setDoc = db.collection('cities').doc('LA').set(data);
Example 2: firestore update array
let washingtonRef = db.collection('cities').doc('DC');
// Atomically add a new region to the "regions" array field.
let arrUnion = washingtonRef.update({
regions: admin.firestore.FieldValue.arrayUnion('greater_virginia')
});
// Atomically remove a region from the "regions" array field.
let arrRm = washingtonRef.update({
regions: admin.firestore.FieldValue.arrayRemove('east_coast')
});
Example 3: firebase cloud method update
firebase deploy --only functions:functionNameHere
Example 4: document.set() firebasefirestore java
var washingtonRef = db.collection("cities").doc("DC");// Atomically add a new region to the "regions" array field.washingtonRef.update({ regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")});// Atomically remove a region from the "regions" array field.washingtonRef.update({ regions: firebase.firestore.FieldValue.arrayRemove("east_coast")});
Example 5: 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"});