hiworldd
hiworldd

Reputation: 17

I want to delete the array stored in Firestore

I received the user's email and designated it as the name of the document, and stored the user information in the document in an array. I tried running my code, but only the Toast message is executed, not deleted.

I have listed the arrays as a list using RecyclerView. Deletion will be implemented through showPop() method.

This is my Firestore database enter image description here I want to delete index-0

  private void showPopup(View v,final int position) {  

  PopupMenu popup = new PopupMenu(activity, v);
  popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    .....
                    case R.id.delete: //delete
                        final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                        final FirebaseFirestore db = FirebaseFirestore.getInstance();
                        final Map<String, Object> updates = new HashMap<>();
                        updates.put(user.getEmail(),FieldValue.delete());

                        //db.collection("medicinday").document(mDataset.get(position).getId())
                          //      .delete()
                        db.collection("medicinday").document(user.getEmail()).update("add_day", FieldValue.arrayRemove(user.getEmail()))
                                .addOnSuccessListener(new OnSuccessListener<Void>() {
                                    @Override
                                    public void onSuccess(Void aVoid) {
                                        Toast.makeText(....
                                    }
                                })
                                .addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                 ....

Upvotes: 1

Views: 1281

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

There is no way you can delete/update an element at a specific index in a Firestore array. For more info, please check my answer from the following post:

Is there any way to update a specific index from the array in Firestore

In your particular case, the add_day property is an array that holds objects. If you want to remove an item from that array, you need to pass the entire object value of the item. It will not work if you just pass an index or one of the nested values in that item.

For your type of data, you'll need to build a Map with the exact content of your first item and pass that object to the update() method, like in the following lines of code:

String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference medicinDayRef = rootRef.collection("medicinday");
DocumentReference emailRef = medicinDayRef.document(email);
HashMap<String, Object> doc = new HashMap<>();
doc.put("day", "monday");
doc.put("king" , "A");
doc.put("nam" , "ren");
doc.put("time1" , "AM 7:30");
doc.put("time2" , "PM 2:30");
doc.put("time3" , "PM 8:30");
emailRef.update("add_day", FieldValue.arrayRemove(doc));

Be aware that if you don't know the entire item's contents, it will not work.

Another option might be to read the document, modify the list on the client by removing the item at the position one, write it modified list back to the document, and in the end simply write it back to Firestore.

More info about this topic can be found here:

Android Firestore querying particular value in Array of Objects

And in the following article:

How to map an array of objects from Cloud Firestore to a List of objects?

Upvotes: 2

ddworks
ddworks

Reputation: 154

It seems like your line to delete from your Firestore is commented out, unless that is intentional for now.

From the picture you have sent, I understand that add_day is your array and 0 is the element you would like to delete. You could do so by accessing your document's field add_day, then using arrayRemove() to specifically delete the element of your choice.

Roughly referring to the linked documentation, I believe you could achieve this by running the following.

DocumentReference doc = db.collection("medicinday").document("[email protected]");

ApiFuture<WriteResult> remove = doc.update("add_day", FieldValue.arrayRemove("0"));

System.out.println("Update time : " + arrayRm.get());

Upvotes: 0

Related Questions