【问题标题】:Firestore adding hashmaps to a map field in FirestoreFirestore 将哈希图添加到 Firestore 中的地图字段
【发布时间】:2019-03-04 23:24:05
【问题描述】:

我正在尝试使用键-> 值对在我的 Firestore 数据库中创建对象映射。 这个想法是在我的 Properties 文档中拥有一张房间对象的地图,其中客厅是关键,而对象则是有价值的。如下图所示

我迷失了将对象添加到 Firestore 的正确方法,因为房间地图已经存在,那么如何向其中添加键->值对? 我还需要在下面的代码中执行搜索,以便我可以获取 Properties 文档并将对象添加到房间地图字段中

FirebaseFirestore db = FirebaseFirestore.getInstance();
final CollectionReference propertyRef = db.collection("Properties");

final Room room = new Room(roomName, feet, inches, imageUrl);

propertyRef.whereEqualTo("propertyId", propertyId).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {

@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
     if (task.isSuccessful()) {
          for (QueryDocumentSnapshot doc : Objects.requireNonNull(task.getResult())) {
              propertyRef.document(doc.getId())
     ----->   .update("rooms", ""+roomName+"", room);

              Log.d(TAG, "Firebase Success= " + imageUrl);
              Toast.makeText(CreatePropertyActivity3.this, "Property Created", Toast.LENGTH_LONG).show();
              exProperties();
              }
              } else {
                  Toast.makeText(CreatePropertyActivity3.this, "Error check log", Toast.LENGTH_LONG).show();
     }
   }
 });

【问题讨论】:

    标签: android google-cloud-firestore


    【解决方案1】:

    db 上使用document(String) 方法,如果文档不存在(https://firebase.google.com/docs/firestore/manage-data/add-data),根据文档将创建该方法

    Map<String, Object> room = new HashMap<>();
    room.put("feet", "...");
    ...
    
    db.collection("rooms").document("the new room id")
            .set(room)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                      ...
                }
            })
            .addOnFalureListener(...)
    

    如果您知道文档的 ID,或者想自己设置 ID。如果是这样,您可以在添加新项目之前替换传递给.document(...) 的参数。或者,您可以使用 add() 方法,该方法将为您创建一个具有自动生成 ID 的新文档。

    在您的情况下,您似乎正在设置自己的有意义的 ID(例如客厅、厨房),并且您应该在添加地图之前更改 propertyId 变量。但是,这是多余的,因为您已经有一个描述房间的属性(即名称)。所以请使用add() 并避免查询一开始就不存在的文档:

    final HashMap<String, Object> newRoom = new HashMap<>();
    newRoom.put(roomName, room);
    ...
    propertyRef.add(newRoom)
        .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
            @Override
            public void onSuccess(DocumentReference documentReference) {
                Log.d(TAG, "DocumentSnapshot written with ID: " + documentReference.getId());
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.w(TAG, "Error adding document", e);
            }
        });
    

    事实上,因为您使用的是whereEqualTo,所以您总是获取对同一文档的引用并覆盖其内容。只需使用 add() 功能并查看文档以获取更多示例。希望对您有所帮助!

    【讨论】:

    • 我很抱歉没有更清楚地解释,请参阅我更新的问题。我正在使用 whereEqualTo 来获取正确的 Properties 文档,然后我可以添加到该文档中的房间地图字段
    猜你喜欢
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 2012-09-03
    • 2022-01-08
    • 2021-07-20
    • 2020-12-14
    相关资源
    最近更新 更多