关于Cloud Firestore中的查询
我正在使用Flutter对Cloud Firestore进行一些研究.目前,我正在调查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 :
第二(Some Dumb Shit)和第三(Some Good Shit)项目属于硬件"领域. .....因此,如果我想根据其领域搜索项目......我会做这样的事情:
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")
但是要说我是否要根据成员的名称搜索项目(请参阅上面的屏幕截图.....我需要搜索名称为"Sarvesh Dalvi"的项目,其中包含"Sarvesh Dalvi".成员"字段).在这种情况下,我应该如何编写查询.
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.
注意:
名称("Sarvesh Dalvi")出现在此层次结构中: DocumentID(例如:"Some Dumb Shit")/Array("Members")/Map({ Name:__ ,MemberRef:__});
Name ("Sarvesh Dalvi") is present inside this heirarchy : DocumentID(eg : "Some Dumb Shit") / Array("Members") / Map({Name : __ , MemberRef :__});
预先感谢您的帮助.
更新:
我学习了如何通过执行以下操作来访问数组:
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]
我只想查询地图内的名称,而不要查询MemberRef……,所以如何查询:
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);});
});
}
无法仅在当前数据结构中查询成员名称.
There is no way to query for just the member name in your current data structure.
您有两个主要选择:
If you also know the
MemberRef
for the member, you can query witharray-contains
:where('Members', arrayContains: {'Name':'New User','MemberRef':'value of memberref'})
. This works becausearray-contains
can check if the array contains the complete value that you specify.
如果您不知道MemberRef
子字段,则需要更改数据模型以允许查询.我通常建议创建一个仅包含成员名称的附加字段:MemberNames: ["New User", "Sarvesh Dalvi"]
.然后,您可以使用相同的array-contains
运算符,但是现在在此新字段上使用简单类型:where('MemberNames', arrayContains: 'New User')
.
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')
.