我想查找属于用户的组。
并且我想查找属于某个组的用户。
(例如ManyToMany)
如何制作模型?
我尝试过:
[]是一个集合
{}是文档
users : [
"uid1" {
"name" : "jack",
"age" : 22,
"groups" : [1, 2]
},
"uid2" {
"name" : "jack",
"age" : 22,
"groups" : [1, 2]
},
"uid2" {
"name" : "hori",
"age" : 21,
"groups" : [3, 4]
},
"uid3" {
"name" : "dora",
"age" : 31,
"groups" : [3]
}
]
groups : [
"1" {
"title" : "science",
"memberCount" : 22,
"members" : ["uid1", "uid2", "uid3"]
},
"2" {
"title" : "history",
"memberCount" : 34,
"members" : ["uid5", "uid7"]
},
"3" {
"title" : "math",
"memberCount" : 34,
"members" : ["uid4", "uid8", "uid11"]
},
"4" {
"title" : "english",
"memberCount" : 52,
"members" : ["uid2", "uid3", "uid7"]
}
]
对吗?如果是,如何查询?
答案 0 :(得分:1)
您应将[]中的集合“成员”更改为类型“数组”:
groups : [ // remain collection
"1" {
"title" : "science",
"memberCount" : 22,
"members" : ["uid1", "uid2", "uid3"] // not in collection but a real array Type
}
]
然后您可以查询属于用户的组:
CollectionReference groupsRef = db.collection("groups");
groupsRef.whereArrayContains("members", "uid2");
更多详细信息:https://firebase.google.com/docs/firestore/query-data/queries#array-contains