Firestore文档引用数组

时间:2018-03-26 09:36:53

标签: java android firebase google-cloud-firestore

我的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

我希望能够从视频集中的数组字段获取产品集合的文档参考,以获取此集合的某些字段的值(例如,产品名称)。

目前我使用此代码:

    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]

有关如何从此阵列获取Product集合的任何字段的任何建议吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

此代码允许我达到我需要的结果

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());
                                            }
                                        }
                                    }
                                });
                    }
                }
            });