How to perform compound queries with logical OR in Cloud Firestore?

后端 未结 9 1657
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 02:07

From the docs:

You can also chain multiple where() methods to create more specific queries (logical AND).

How can I perform an <

9条回答
  •  执念已碎
    2020-11-22 02:27

    OR isn't supported

    But if you need that you can do It in your code

    Ex : if i want query products where (Size Equal Xl OR XXL : AND Gender is Male)

    productsCollectionRef
                    //1* first get query where can firestore handle it
                    .whereEqualTo("gender", "Male")
                    .addSnapshotListener((queryDocumentSnapshots, e) -> {
    
                        if (queryDocumentSnapshots == null)
                            return;
    
                        List productList = new ArrayList<>();
                        for (DocumentSnapshot snapshot : queryDocumentSnapshots.getDocuments()) {
                            Product product = snapshot.toObject(Product.class);
    
                            //2* then check your query OR Condition because firestore just support AND Condition
                                if (product.getSize().equals("XL") || product.getSize().equals("XXL"))
                                    productList.add(product);
                        }
    
                        liveData.setValue(productList);
    
                    });
    

提交回复
热议问题