Sarvesh Dalvi
Sarvesh Dalvi

Reputation: 2718

Regarding querying in cloud firestore

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 :

enter image description here

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);});
    });
  }

Upvotes: 1

Views: 99

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600130

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').

Upvotes: 2

Related Questions