Raffaele Rossi
Raffaele Rossi

Reputation: 3127

Flutter firestore structure for query condition

I am new to NoSQL and I'm trying to figure out a good way to represent my data. I have a series of workers that need to request vacations via mobile app.

enter image description here

When I try to write a Firebase query with Flutter, I can do this:

Firestore.instance
    .collection("ferie_permessi")
    .document("[email protected]")
    .snapshot();

It works but there are two main errors:

  1. If I try to create another collection called "Woker info" I cannot use [email protected] as document ID as it already esists;
  2. I have to sort data client side because firestore doesn't give me the possibility (with this setup I've made).

I'm quite sure that this structure isn't good at all. Each worker needs to have 2 lists: vacations and other. What is wrong?

My guess is that I should move [email protected] together with vacations and other so that I can make a query of this kind:

Firestore.instance
    .collection("ferie_permessi")
    .where("user", "==", "[email protected])
    .snapshot();

But now the id? Is an automatic one good?

Upvotes: 0

Views: 127

Answers (1)

Abhishek Kanojia
Abhishek Kanojia

Reputation: 837

I had a chance to recently explore creating an app using a firebase-firestore. A couple of things will help here:

  1. Yes, the autogenerated id is good since it is unique, for example, you can have a collections vacation_requests, users you can then use that user_id as a document in vaccation_requests -> user_id -> vacations, instead of using email as a document key.

Or

You can do it like this collections users, vacation_requests, and requests.

  1. store user details in users.
  2. store requests in requests with from and to dates.
  3. store reference of User and Request in vaccation_requests.

Hope this helps!

Upvotes: 1

Related Questions