簡體   English   中英

Firestore 數據庫 java 在獲取集合之前添加 where 條件

[英]Firestore database java add where condition before get collection

我正在為我的 web 應用程序使用 Firestore 數據庫。 當我從集合中查詢文檔時,我需要 append 一個 where 條件。 append具體條件在fetch CollectionReference之前有什么方法可用嗎?

CollectionReference collectionReference = dbFirestore.collection("collectionName").someMethod("Where conditions to fetch specific documents")

或任何其他選項將條件為Java 或 Spring的完整查詢發送到Firestore並獲得結果? 我不想在我的前端設計中使用數據庫代碼。 如果沒有其他選擇,我最終會使用 javascript 來這樣做。 但這不是一個合適的設計。

Query query = dbFirestore.collection("collectionName").whereEqualTo("colmn1", "value").whereEqualTo("column2", "value");

如何從查詢 (com.google.cloud.firestore.Query) object 中迭代值?

如果您需要獲取以下查詢的結果:

Query query = dbFirestore.collection("collectionName").whereEqualTo("colmn1", "value").whereEqualTo("column2", "value");

您必須調用get() ,然后附加一個偵聽器,如您在以下代碼行中所見:

query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                if (document != null) {
                    //Get the data out from the document.
                }
            }
        } else {
            Log.d(TAG, task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

如果您將 Java 用於非 Android 項目,則應考慮使用以下代碼行:

ApiFuture<QuerySnapshot> future = dbFirestore.collection("collectionName").get();
QuerySnapshot querySnapshot = future.get();
List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
ArrayList<String> ids = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();
for (QueryDocumentSnapshot document : documents) {
    String docId = document.getId();
    ids.add(docId);
    System.out.println(docId);
    String name = document.getString("name");
    names.add(name);
    System.out.println(name);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM