C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Update and Delete in FirestoreLike the Firebase real-time database, we can update and delete the values from the Firebase Firestore. Update:For updating some fields of a document without overwriting the entire document, use the update() method. This method is used in the following way: val lucknowRef=db.collection("states").document("UP") //Setting the "isCapital" field of the state 'UP' lucknowRef.update("capital", true) .addOnSuccessListener{Log.d(TAG,"DocumentSnapshot successfully updted")} .addOnFailureListener{(e-> Log.w(TAG, "Error updating document",e)} We can use "dot nation" to reference nested fields within the document, if our documents contain nested object,. //Suppose we have the document which contains: //{ // name:"Shubham" // favorites:{food:"Pizza",color:"Blue",subject:"Physics"} // age:21 //} //For updating age and favorite color: db.collection("users").document("Shubham") .update(mapOf( "age" to 21 "favorites.color" to "Red" )) For adding and removing elements from an array, arrayUnion() and arrayRemove() methods are available. The arrayUnion() adds elements to an array but only those elements that are not already present. The arrayRemove() methods remove all instances of each given element. val lucknowRef=db.collection("state").document("UP") //Adding a new region to the "regions" array field automatically. lucknowRef.update("regions",FieldValue.arrayUnion("greater_virginia")) //Removing a region to the "regions" array field automatically. lucknowRef.update("regions",FieldValue.arrayRemove("east_coast")) Delete:In Firebase Firestore, for deleting a document, the delete() method is used: db.collection("states").document("UP") .delete() .addOnSuccessListener{Log.d(TAG,"DocumentSnapshot successfully deleted!")} .addOnSuccessListener{e->Log.w(TAG,"Error deleting document",e)} It does not automatically delete the documents within its subcollections. We can still access the sub-collection documents by reference. E.g. we can access the document at path /mycoll/mydoc//mysubcoll/mysubdoc even if we delete the ancestor document at /mycoll/mydoc. If we want to delete a document and all the documents within its sub-collection, we must do so manually. Deleting FieldsFor deleting a specific field from a document, use the FieldValue.delete() method when we update a document. val docRef=db.collection("state").document("UP") //remove the capital field from the document val updates=hashMapOf Deleting CollectionsWe have to retrieve all the documents within the collection or sub-collection and delete them to delete an entire collection or sub-collection. For larger collections, we may want to delete the documents in smaller batches for avoiding out-of-memory errors. Deleting a collection requires coordinating an unbounded number of individual delete requests, and deleting a collection from a mobile client has negative security and performance implications. Update/Delete//Creating helper method updateUser private fun updateUser(name: String, email: String) { // updating the user via child nodes if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(email)) { val myRef = mFirebaseDatabaseInstance?.collection("users")?.document(userId!!) myRef!!.update("name", name) myRef.update("email", email) Toast.makeText(applicationContext, "Successfully updated user", Toast.LENGTH_SHORT) .show() } else Toast.makeText(applicationContext, "Unable to update user", Toast.LENGTH_SHORT).show() } //Event handler for update fun onUpdateClicked(view: View) { //Getting value from text field val name = username.getText().toString() val email = email.getText().toString() //Calling helper updateUser method updateUser(name, email) getDataOneTime() } //Event handler for delete fun onDeleteClicked(view: View) { //Delete user using delete() method mFirebaseDatabaseInstance!!.collection("users").document(userId!!).delete() .addOnSuccessListener { Toast.makeText(applicationContext, "Successfully deleted user", Toast.LENGTH_SHORT).show() } .addOnFailureListener { Toast.makeText(applicationContext, "Unable to delete user", Toast.LENGTH_SHORT).show() } // clear information txt_user.setText("") email.setText("") username.setText("") FirebaseAuth.getInstance().signOut() startActivity(Intent(this, MainActivity::class.java))}
Next TopicFirebase Cloud Storage
|