Firestore query to check if an id is in a collection and then going to the subcollection in a single query

huangapple go评论58阅读模式
英文:

Firestore query to check if an id is in a collection and then going to the subcollection in a single query

问题

我有一个Firestore有两个根集合,分别是"properties"和"users"。每个属性都有一个名为"huurders"的用户列表,其中只包含userId,并且每个属性还有一个名为"rooms"的子集合,其中每个房间也有一个包含UserId的"huurder"字段。

我正在尝试获取所有"y"字段等于给定id的房间列表。目前我只是检查"y"是否包含我正在寻找的UserId,但我不知道如何进入子集合"x"。

这是我的函数:

override fun get(userId: String): Flow<RoomsResponse> = callbackFlow {
    val snapshotListener = dbRef.collection("properties").whereArrayContains("huurders", userId).addSnapshotListener { snapshot, e ->
        val roomsResponse = if (snapshot != null) {
            val rooms = snapshot.toObjects(Room::class.java)
            Response.Success(rooms)
        } else {
            Response.Failure(e)
        }
        trySend(roomsResponse)
    }
    
    awaitClose { snapshotListener.remove() }
}

这是不正确的,因为它只会返回包含UserId的属性列表。我需要深入一层,获取房间。我的Firestore将会链接成一个图像。

英文:

I have a firestore with 2 root collections, properties and users. Each of these properties has a list of "huurders" which are just userId's, and a subcollection of rooms each of which also has a "huurder" field which contains a UserId.

I'm trying to get a list of all rooms where the "y" field is equal to a given id. Right now I'm just checking if "y" contains the UserId i'm looking for, but I'm struggling about how to go into the subcollection x afterwards.

This is my function

override fun get(userId: String): Flow&lt;RoomsResponse&gt; = callbackFlow {
    val snapshotListener = dbRef.collection(&quot;properties&quot;).whereArrayContains(&quot;&quot;, ).addSnapshotListener() { snapshot, e -&gt;
        val roomsResponse = if (snapshot != null) {
            val rooms = snapshot.toObjects(Room::class.java)
            Response.Success(rooms)
        } else {
            Response.Failure(e)
        }
        trySend(
    awaitClose { snapshotListener.remove()
}
}

This is incorrect given this would just return me a list of properties where the is in the array . I need to go one layer deeper and get the rooms. My firestore will be linked as an image

答案1

得分: 0

Firestore查询总是返回您查询的文档。它们无法返回嵌套集合中的文档或其他链接的数据。

所以,如果您想从数据库中获取一个房间列表,您需要查询rooms子集合。要查询名为rooms的所有集合,您可以使用集合组查询。而且,由于您似乎在每个rooms文档中都有一个huurder字段,您应该能够通过以下方式获取正确的结果:

dbRef.collectionGroup("rooms").whereEqualTo("huurder", userId)

这将为您提供一个rooms文档快照列表。

如果您然后想要查找这些结果中特定DocumentSnapshot的属性,您可以获取其DocumentReference,然后检查其父级父级

英文:

Firestore queries always returns the documents that you query on. They can't return documents from nested collection, or otherwise linked data.

So if you want to get a list rooms from the database, you'll need to query the rooms subcollection. To query across all collections named rooms, you can use a collection group query. And since you seem to have a huurder field in each rooms document, you should be able to get the correct results with:

dbRef.collectionGroup(&quot;rooms&quot;).whereEqualTo(&quot;huurder&quot;, userId)

This will give you a list of rooms document snapshots.


If you then want to find the property of a specific DocumentSnapshot in these results, you can get its DocumentReference and then check the parent of its parent.

huangapple
  • 本文由 发表于 2023年4月17日 18:02:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定