cenk göktay
cenk göktay

Reputation: 1

How to get document with certain fields from firebase to flutter?

I need a flutter query for getting data from firebase;

The query should go to User collection and search every document with UserType = 1 field. There is a "classes" collection in each document and each of "classes" have other documents such as "math", "music" etc. I need to get the document with the field classCode == myInput

QuerySnapshot query1 = await _fireStore.collection("Users").where(["userType"], isEqualTo: 1).get();

I don't know how to move from here.

Upvotes: 0

Views: 130

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599776

You have two main options for implement this.

The first is pretty close to how you describe in your question:

  1. Find the user docs for your condition,
  2. For each of those search the classes subcollection for the classCode.

The second approach makes use of a collection group query to:

  1. Find all classes docs (across all the subcollections) with the searched user type.
  2. Possibly then look up the users for those.

The best approach depends on mostly on what results you need. If you results are user documents, the first approach is usually pretty easy. If the results are classes then I'd definitely try the second approach first.

Upvotes: 1

Related Questions