如何在Firebase文档中查询数组列表?

huangapple go评论67阅读模式
英文:

How to query an array list in a Firebase document?

问题

我正在尝试查询我的数据库以供Android应用程序使用,如果一个用户名在我的数据库的'fave'数组字段中,然后更改图像的背景。我的数据库设置如下...

如何在Firebase文档中查询数组列表?

我不知道我是否做得对,但目前我认为我可能正在检查整个集合,而不是特定的文档,而且它甚至没有返回任何内容。任何建议将不胜感激!

英文:

I'm trying to query my database for an android application, if a username is in the 'fave' array field of my database and if so then change the background of an image. My database is set up like this...

如何在Firebase文档中查询数组列表?

I don't know if i'm doing it right but currently i think i may be checking the whole collection rather than a specific document and it's not even returning anything. Any suggestions would be appreciated!

答案1

得分: 1

FirebaseFirestore.getInstance().collection("Trainers").document("Air Force 1 Low").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        List<String> list = (List<String>) documentSnapshot.get("fave");
        for (String name : list){
            if (name.equals("Admin")){
                // 如果用户是管理员则执行以下操作
                return;
            }
        }
        // 如果用户不是管理员则执行其他操作
    }
});
英文:

You wanted this?

FirebaseFirestore.getInstance().collection(&quot;Trainers&quot;).document(&quot;Air Force 1 Low&quot;).get().addOnSuccessListener(new OnSuccessListener&lt;DocumentSnapshot&gt;() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        List&lt;String&gt; list = (List&lt;String&gt;)documentSnapshot.get(&quot;fave&quot;);
        for (String name : list){
            if (name.equals(&quot;Admin&quot;)){
                //do if user is admin
                return;
            }
        }
        //do if user is not admin
    }
});

huangapple
  • 本文由 发表于 2020年4月10日 02:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61127753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定