问题
I'm doing some research on cloud firestore using flutter. Currently I'm looking into querying in cloud firestore. I got a basic idea of how to query like say in the screenshot of the database given below :
The 2nd (Some Dumb Shit) and 3rd (Some Good Shit) projects belong to the field "Hardware" ..... So if I want to search a project with respect to its field ..... I'll do something like this :
databaseReference.collection("Projects").where("Field",isEqualTo: "Hardware")
But say if I want to search projects based on the name of the members ( Referring to the screenshot above ..... I need to search of a project where a name "Sarvesh Dalvi" is present inside the "Members" field). How am I supposed to write a query in this case.
Note :
Name ("Sarvesh Dalvi") is present inside this heirarchy : DocumentID(eg : "Some Dumb Shit") / Array("Members") / Map({Name : __ , MemberRef :__});
Thanks in advance for the help.
Update :
I learned how to access an array by doing something like this :
Future<dynamic> getUserProjectFromDatabase(String username)
{
return databaseReference.collection("Projects").where("Members",arrayContains: {"Name" : username}).getDocuments().then((value){
print(value.documents.length);
value.documents.forEach((element) {print(element.data);});
});
}
But this works if the Map only contains :
{"Name" : username};
But in my case my Map is something like this :
{
"Name" : username,
"MemberRef" : /*Reference to a Document*/
};
[Refer to the screenshot posted above]
I only want to query for the Name inside the map and not the MemberRef...... so how can I query something like :
Future<dynamic> getUserProjectFromDatabase(String username)
{
return databaseReference.collection("Projects").where("Members",arrayContains: {"Name" : username,"MemberRef" : /* can be anything */}).getDocuments().then((value){
print(value.documents.length);
value.documents.forEach((element) {print(element.data);});
});
}
回答1:
There is no way to query for just the member name in your current data structure.
You have two main options:
If you also know the
MemberReffor the member, you can query with array-contains:where('Members', arrayContains: {'Name':'New User','MemberRef':'value of memberref'}). This works becausearray-containscan check if the array contains the complete value that you specify.If you don't know the
MemberRefsubbfield, then you'll need to change your data model to allow the query. I typically recommend creating an additional field with just the member names:MemberNames: ["New User", "Sarvesh Dalvi"]. Then you can use the samearray-containsoperator, but now on this new field with the simple type:where('MemberNames', arrayContains: 'New User').
来源:https://stackoverflow.com/questions/62834339/regarding-querying-in-cloud-firestore