Regarding querying in cloud firestore

夙愿已清 提交于 2020-08-05 10:13:50

问题


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:

  1. If you also know the MemberRef for the member, you can query with array-contains: where('Members', arrayContains: {'Name':'New User','MemberRef':'value of memberref'}). This works because array-contains can check if the array contains the complete value that you specify.

  2. If you don't know the MemberRef subbfield, 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 same array-contains operator, 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

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