繁体   English   中英

Firestore文档参考数组

[英]Firestore Array of Document References

我的Firestore数据库具有以下结构:

products:{ // Collection
    procuct_1: { // Document
       title:"",
       url:""

videos:{ // Collection
    video_1:{ // Document
       title:"",
       products:{ // Array of references 
            0: "/products/product_1,
            1: "/products/product_2

我希望能够从Videos集合中的 Array字段获取对Products集合的文档引用,以便获取此集合的某些字段的值(例如产品标题)。

目前,我使用以下代码:

    FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    firebaseFirestore
            .collection("videos")
            .get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot documentSnapshot = task.getResult();

                        Map<String, Object> map = documentSnapshot.getData();
                        for (Map.Entry<String, Object> entry: map.entrySet()){
                            if (entry.getKey().equals("products")){
                               textView.setText(entry.getValue().toString());
                            }
                        }

但是entry.getValue()。toString()向我返回了这样的数组:

[com.google.firebase.firestore.DocumentReference@451d5ae8,

com.google.firebase.firestore.DocumentReference@e28cd0c]

关于如何从此数组获取产品集合的任何字段的任何建议?

要解决此问题,请使用以下代码:

firebaseFirestore.collection("videos")
    .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            for (DocumentSnapshot document : task.getResult()) {
                List<String> products = (List<String>) document.get("products");
                for (String product : products) {
                    Log.d("TAG", product);
                }
            }
        }
    });

输出将是:

/products/product_1
/products/product_2

此代码使我可以达到所需的结果

FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    firebaseFirestore
            .collection("videos")
            .get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    DocumentSnapshot document = task.getResult();
                    ArrayList<DocumentReference> products = (ArrayList<DocumentReference>) document.get("products");
                    for (int i = 0; i < products.size(); i++) {
                        DocumentReference ref = products.get(i);
                        ref.get()
                                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                        if (task.isSuccessful()) {
                                            DocumentSnapshot document = task.getResult();
                                            if (document != null && document.exists()) {
                                                BestBuysProducts best_buy = document.toObject(BestBuysProducts.class);
                                                Log.d(TAG, "Title: " + best_buy.getProduct_title());
                                            }
                                        }
                                    }
                                });
                    }
                }
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM