我想删除存储在Firestore中的数组 [英] I want to delete the array stored in Firestore
问题描述
我收到了用户的电子邮件,并将其指定为文档的名称,并将用户信息以数组形式存储在文档中.我尝试运行我的代码,但仅执行Toast消息,而不是将其删除.
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.
我已经使用 RecyclerView
将数组列为列表.删除将通过 showPop()
方法实现.
I have listed the arrays as a list using RecyclerView
. Deletion will be implemented through showPop()
method.
这是我的Firestore数据库在此处输入图片描述我要删除索引-0
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) {
....
推荐答案
无法删除/更新Firestore数组中特定索引处的元素.有关更多信息,请从以下帖子中查看我的回答:
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:
在您的特定情况下,
add_day
属性是一个包含对象的数组.如果要从该数组中删除一个项目,则需要传递该项目的整个对象值.如果仅传递索引或该项目中的嵌套值之一,它将不起作用.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.对于您的数据类型,您需要使用第一个项目的确切内容构建一个
Map
并将该对象传递给update()
方法,就像下面的代码行一样: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 theupdate()
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.
另一种选择可能是读取文档,通过删除第一位置的项目来修改客户端上的列表,将修改后的列表写回到文档中,最后简单地将其写回到Firestore.
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:
在以下文章中: