无法从Firestore中删除字段

时间:2020-06-20 04:10:45

标签: java android firebase google-cloud-firestore

我无法从Firestore中删除字段

else if(starPosition == 0){
   Map<String, Object> deleteRating = new HashMap<>();
   deleteRating.put(inititalRating + "_star", documentSnapshot.getLong(inititalRating + "_star") - 1);
   deleteRating.put("average_rating", String.valueOf(calculateAverageRating(starPosition-inititalRating,true)));
   deleteRating.put("total_ratings", documentSnapshot.getLong("total_ratings") - 1);

   firebaseFirestore.collection("PRODUCTS").document(productID)
           .update(deleteRating).addOnCompleteListener(new OnCompleteListener<Void>() {
       @Override
       public void onComplete(@NonNull Task<Void> task) {
           if (task.isSuccessful()) {
               Map<String, Object> ratingUpdate = new HashMap<>();
               ratingUpdate.put("list_size", DBqueries.myRatedIds.size() - 1);
               ratingUpdate.put("product_ID_" + DBqueries.myRatedIds.size(), FieldValue.delete());
               ratingUpdate.put("rating_" + DBqueries.myRatedIds.size(), FieldValue.delete());

               firebaseFirestore.collection("USERS").document(currentUser.getUid()).collection("USER_DATA").document("MY_RATINGS")
                       .update(ratingUpdate).addOnCompleteListener(new OnCompleteListener<Void>() {
                   @Override
                   public void onComplete(@NonNull Task<Void> task) {

                   }
               });


           }
       }
   });    

}

没有删除我在这里使用的字段FieldValue.delete()

Screenshot firestore fielsd

1 个答案:

答案 0 :(得分:1)

您的计数器减一。您运行:

   Map<String, Object> ratingUpdate = new HashMap<>();
   ratingUpdate.put("list_size", DBqueries.myRatedIds.size() - 1);
   ratingUpdate.put("product_ID_" + DBqueries.myRatedIds.size(), FieldValue.delete());
   ratingUpdate.put("rating_" + DBqueries.myRatedIds.size(), FieldValue.delete());

   firebaseFirestore.collection("USERS").document(currentUser.getUid()).collection("USER_DATA").document("MY_RATINGS")

由于屏幕快照显示list_size为0,因此意味着DBqueries.myRatedIds.size()为1。因此,您要删除{\ n} / {n}字段,而实际上不是/否在文档中出现的时间更长。

如果要删除product_ID_1索引字段,则也需要在其中使用rating_1