查询Firestore数据并将一个字段添加到与查询匹配的所有文档中 [英] Query Firestore data and add one field to all the documents that match the query

查看:44
本文介绍了查询Firestore数据并将一个字段添加到与查询匹配的所有文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个用于出售书籍的Android应用.我的用户可以为二手书发布广告并出售.我打算添加一个功能,我的用户可以选择匿名.将会出现一个名为让我匿名的复选框.如果用户选中该框,则其他人将看不到他们的电话号码和姓名.仅通用名称应可见.

I am building an Android app to sell books. My users can post ads for their used books and sell them. I am planning to add a feature where my users can opt to go anonymous. There will be checkbox with name Make me anonymous. If users check that box, their phone number and name will not be visible to others. Only a generic name should be visible.

现在的问题是,我想在用户上传的每个广告文档中添加一个条目 anonymous = true .

Now the problem is, I want to put an entry anonymous = true in every ad documents that the user uploaded.

我想查询用户投放的广告,并添加一个字段 anonymous = true .我想做类似以下的事情:

I want to query the ads that the user put and add a field anonymous = true. I want to do something like below:

 final CheckBox anonymousCB = findViewById(R.id.anonymousCB);
    anonymousCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (anonymousCB.isChecked()){
                WriteBatch batch = firestore.batch();
                DocumentReference sfRef = firestore.collection("books").whereEqualTo(uid,uid);
                batch.update(sfRef, "anonymous", "true");
            }
        }
    });

但是我无法进行查询并将字段插入与查询匹配的所有文档中.有什么更好的方法吗?

But I cannot make a query and insert a field into all the documents that match the query. Is there any better way to do this?

推荐答案

还有更好的方法吗?

Is there any better way to do this?

是的,有.要解决此问题,请在 onCheckedChanged()方法内使用以下代码行:

Yes, there is. To solve this, please use the following lines of code inside onCheckedChanged() method:

Query sfRef = firestore.collection("books").whereEqualTo(uid, uid);
sfRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            List<String> list = new ArrayList<>();
            for (DocumentSnapshot document : task.getResult()) {
                list.add(document.getId());
            }

            for (String id : list) {
                firestore.collection("books").document(id).update("anonymous", true).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "anonymous field successfully updated!");
                    }
                });
            }
        }
    }
});

此代码的结果是将 anonymous 属性添加到所有 book 对象并将其设置为 true .请注意,我在代码中使用的是布尔值 true not 字符串true.这样使用起来更方便.

The result of this code would be to add the anonymous property to all your book objects and set it to true. Please note, I have used the boolean true and not the String true as you have used in your code. Is more convenient to be used in this way.

PS 如果您正在为书使用模型类,请同时从此

P.S. If you are using a model class for your books, please also see my answer from this post.

这篇关于查询Firestore数据并将一个字段添加到与查询匹配的所有文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆