Java,从具有FireStore数据的Map中获取密钥

时间:2019-03-16 13:36:07

标签: java google-cloud-firestore

我正在将数据从FireStore获取到Map中,我在Firestore中的数据如下所示:

(Auto-Document ID)
    title="title1"
    body="body1"
(Auto-Document ID)
    title="title2"
    body="body2"

当我将它们加载到地图中时,它将如下所示:

  

loadNoteCallBack:{rsmw3EkfIjUQxrzRiyy7 = {body = body2,title = title2},sTEfYCcPK9lw77xWS8H5 = {body = body1,title = title1}}

“ rsmw3EkfIjUQxrzRiyy7”和“ sTEfYCcPK9lw77xWS8H5”是自动生成的文档ID

现在我的问题是,我想从中提取标题,但我不完全知道该怎么做。
我已经尝试过:

List<String> keys = new ArrayList<>();

for (String key : data.keySet()) {
    keys.add(key);
}

但是我以这种方式获得了文档ID。

  

标题:[rsmw3EkfIjUQxrzRiyy7,sTEfYCcPK9lw77xWS8H5]

我应该如何获得标题?

更新

我认为我做到了。是解决这个问题的好方法吗?

    public static void loadNote() {
    db.collection("Datas")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        Map<String, Object> data = new HashMap<>();
                        List<String> titles = new ArrayList<>();
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            titles.add(document.getString("title"));
                            data.put(document.getId(),document.getData());
                        }                            
                        defaultEntitys.loadNoteCb(data,titles);
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

2 个答案:

答案 0 :(得分:1)

更新

选项1:

如果直接使用Firestore查询引用,请使用QueryDocumentSnapshot类方法从Firestore文档获取值

导入

import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.firestore.QueryDocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;

代码

  Firestore db = firestoreOptions.getService();
  ApiFuture<QuerySnapshot> query = db.collection("data").get();
  QuerySnapshot querySnapshot = query.get();
  List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
  ArrayList<String> titles = new ArrayList<>();
  for (QueryDocumentSnapshot document : documents) {
    System.out.println("auto id: " + document.getId());
    String title = document.getString("title");
    titles.add(title);
  }
  System.out.println("titles = " + titles);

选项2:

使用data.entrySet()来获取键值对,如下所示

for (Map.Entry<String, Object> o : data.entrySet()) {
    String key = o.getKey();
    Object value = o.getValue();
}

使用Jackson之类的任何JSON库将值解析为JSON对象或类对象以获取标题。

答案 1 :(得分:1)

使用Java8流的更简洁的方法是:

Map<String,FireStore> map=new HashMap<>();

FireStore f1=new FireStore("title1","body1");
FireStore f2=new FireStore("title2","body2");

map.put("rsmw3EkfIjUQxrzRiyy7",f1);
map.put("sTEfYCcPK9lw77xWS8H5",f2);

List<String> titles = map.entrySet()
        .stream()
        .map(e -> e.getValue())
        .map(FireStore::getTitle)
        .collect(Collectors.toList());

System.out.println(titles);

您的FireStore对象:

public class FireStore {
    private String body;
    private String title;
    public FireStore(String title,String body) {
        this.body = body;
        this.title = title;
    }
    public String getBody() { return body; }
    public void setBody(String body) { this.body = body; }
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
}

作为您从link提供的信息。您需要添加以下更改。

//asynchronously retrieve all documents
ApiFuture<QuerySnapshot> future = db.collection("cities").get();
// future.get() blocks on response
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
List<String> titles=new ArrayList<>();
for (QueryDocumentSnapshot document : documents) {
  titles.add(document.get("title")); //this will get titles and add into titles list.
}