提问者:小点点

FireStore:按子集合中的字段获取文档


这个还行。 但我认为它可以解决不使用用户文档中聊天室的额外子集合。 也许是通过询问。 所以我需要这样的查询:

Query chatrooms = FirebaseFirestore.getInstance()
    .collection("Chatrooms")
    .document() //Here's the problem, I need to query through every document, not just through specific one
    .collection("User List")
    .whereEqualTo("user_id", userId);

这有办法吗?
还有一个问题,这是高效的还是我应该继续使用以前的解决方案?


共1个答案

匿名用户

您要查找的是集合组查询。 这样,您就可以跨所有用户列表子集合查询以查找用户文档。

db.collectionGroup("User List").whereEqualTo("user_id", userId).get()
  .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
      @Override
      public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
          for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
             Log.i("User", document.getId());
             Log.i("Chat room", document.getReference().getParent().getParent().getId());
          }
      }
  });