Can a query be constructed using a list of strings that represent a field in a firestore document?

爱⌒轻易说出口 提交于 2020-01-16 16:57:12

问题


I wish to filter documents based on a list which contains userId's these userId's are fields in the documents I wish to access. this question is similar but instead of a list of document references I have a list of field items.

Currently all I have is a display of all the documents in the collection:

Query posts = db.collection("posts");


        FirestoreRecyclerOptions<Post> options = new FirestoreRecyclerOptions.Builder<Post>()
                .setQuery(posts, Post.class)
                .build();

        adapter = new FirestoreRecyclerAdapter<Post, PostViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull PostViewHolder postViewHolder, int position, @NonNull Post post) {
                postViewHolder.setPost(post);
            }

            @NonNull
            @Override
            public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.card_view_layout, parent, false);
                return new PostViewHolder(view);
            }
        };

        recyclerView.setAdapter(adapter);

I want the output to be all posts with a field userId that is contained in the List.

In other words, I know that I can query all documents with a specific field but can I query all documents that fit a a list of fields?


回答1:


It's currently not possible to make one query return all documents that exist in an existing list. This means you will not be able to use FirestoreRecyclerAdapter to populate your RecyclerView, since it's only capable of taking a single Query object. Instead, you will have to make a query for each one of the documents you want to display, collect the results in memory, and use a different type of adapter to populate the view.



来源:https://stackoverflow.com/questions/56860624/can-a-query-be-constructed-using-a-list-of-strings-that-represent-a-field-in-a-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!