Select object with string contains some substrings from Firebase DB

给你一囗甜甜゛ 提交于 2021-01-29 05:04:52

问题


I have firebase database with structure

Now I want to select only names with given gender and qualities. I wrote code like this

private NameImpl.Gender mGender;
private String[] mQualities;

...

public void GetNames()
{
    mRef.orderByChild("Gender").equalTo(mGender.name()).addValueEventListener(new ValueEventListener()
     {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            getData(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
     });
}

But I can't understand how to add here requirement that Qualities string in database must contain all words from mQualities.


回答1:


You can't do this with Firebase Realtime Database without some significant changes to your schema. Realtime Database doesn't have the capability to perform filtering on multiple conditions (some people say "multiple where clauses" in SQL terms). If you want to check for matches on multiple things, you'll have to create a composite field that contains a combination of the things you're looking for. For example, you'll need a string value that has gender and all the qualities concatenated together in predictable fashion, then have clients search for that string.

Firestore allows you to filter on multiple conditions, but you still can't search for sets of objects. You'll still need a special field that concatenates all the qualities together.



来源:https://stackoverflow.com/questions/50337856/select-object-with-string-contains-some-substrings-from-firebase-db

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