0

I have an app, in which a user can create a session for themselves. By a session I mean , it has a title and a frequency (For Example - {"title": "homework" , "frequency": "MWF"} where MWF is Monday,Wednesday,Friday). I want to store the session frequency and title in my firestore database in such a way that when the user creates a new session, i fetch from firestore the possible clashes -

For example - if the user is creating a session called "homework" and an "homework" already exists then , I can tell them that a session by that name already exists OR If a user is creating a session with frequency" MTS and already has a session with the frequency MWF, then I have to tell the user that there is a possible clash. Same with creating TTF and MWF already exists, then I have to inform the user of a possible clash.

My question, is that how can i do it, given the fact that I use the firestore database?

Aryaman Shrey
  • 93
  • 1
  • 9

1 Answers1

1

The easiest thing to do is simply query for any documents that would conflict before you allow the user to add the conflicting document. So, if you want to find a conflict where title="homework", you would query for conflicts first:

firestore.collection("your-collection").where("title", "==", "homework")
// then check the results to see if there was a match

Firestore doesn't provide a way to stop duplicate field values in a collection, so it's still possible that somehow a user could add a conflicting document. If you need to force uniqueness of a field value within a collection, that requires much more work, which might not be worthwhile for your case.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • what about the **OR** part at the end of the question? – Aryaman Shrey Aug 19 '20 at 20:35
  • Then do another query for the second requirement. [Firestore does not support logical OR queries involving multiple fields.](https://stackoverflow.com/questions/47018212/implementing-or-in-firestore-query-firebase-firestore) – Doug Stevenson Aug 19 '20 at 21:04