0

This is my first time using a NOSQL database and I'm really struggling to work out how to structure my data.

I have an app that predicts a users mood and then the user can select if that's right or not. So I need to save both the prediction and the actual result. I want to be able to pull the latest result from firebase and display it on the app.

I understand how I'd do this on an SQL DB and understand how to write an SQL query to get that data back out.

For my Firebase DB I thought of the following structure

the document name is the usersID and store multiple arrays based on the timestamp but I can't seem to user OrderBy on a document only a collection so not sure how to get this back.

The fact that this seems so difficult less me to believe I've implemented the DB wrong to begin with.

Structure of DB is as follows: enter image description here

I should add that it all works fine for the USER_TABLE as its one document id and a single entry, so I've no problem retrieving that.

Thanks for your help!

1 Answer 1

2

orderBy is an instruction to the database to order documents on the server, before it returns them to your app. To store the fields inside the document, you can just do that inside your application code after it receives the document(s).

There is in itself nothing wrong with storing these entries in a single document, Just keep in mind that:

  • A document can be at most be 1MB in size, so make sure this fits your maximum number of entries.
  • Firestore only ever returns full documents, so you will either get all entries in a document, or none of them.
  • You won't be able to order or filter the entries inside a single document. If that is a requirement for you, consider storing each entry in its own document in a subcollection. Note that this will increase the number of documents each user reads though, which will increase the cost.
Sign up to request clarification or add additional context in comments.

3 Comments

@frankvanpudffelen thanks for the detailed response. The only returning whole documents makes sense. Would a better approach be to make a new document every time. Add a time stamp and orderBy desc time and add a conditional where to filter by userID to retrieve the latest entry?
"better" depends on your needs, but that would allow you to for example filter in a query (at a higher cost).
I think it would definitely satisfy the needs of the project I believe. What is the impact of such a cost? I’m estimating an average of 2 eateries per user per day. So say roughly 750 documents per user per year. Is that an overhead that is unmanageable at scale? I can’t seem to think of any other approaches

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.