Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Empty Documents in Firestore

I understand that empty documents within collections are removed automatically by the system in Firestore. However, I have a situation now where the name of the document serves a purpose. I have a collection named usernames, and within this, many documents with the ID being the username. For instance, usernames/bob_marley is what I might see in the database. The problem here is that, since the documents do not have any fields in them, they get removed automatically thereby defeating the purpose of the set-up. How should I be structuring my database in these cases?

Thank you

like image 885
Apper Avatar asked Feb 13 '18 02:02

Apper


People also ask

Can I create an empty collection in firestore?

In Firestore there's just no such thing as an "empty collection" and we have no mechanism today to "create" collections or "delete" collections other than by writing a document (to "create" the collection) and deleting all the documents (to "delete" the collection).

How many documents can I have in a collection firestore?

20 for multi-document reads, transactions, and batched writes. The previous limit of 10 also applies to each operation. For example, imagine you create a batched write request with 3 write operations and that your security rules use 2 document access calls to validate each write.

Can two documents have the same ID in firestore?

New document with same ID should not be allowed in the same collection. It should be possible to fetch an already existing document from a previous import.


1 Answers

The easiest thing to do is simply not allow the document to ever become empty. Keep one property in it with (for example) "exists = true" and make sure it never gets removed. Use a security rule to prevent this, if you're concerned about users accidentally doing this to themselves.

Another thing to do is re-evaluate what exactly you're trying to do with an empty document in the system, and if it's worthwhile to think about how to structure your data in a way that best meets the queries you want to perform.

like image 183
Doug Stevenson Avatar answered Oct 23 '22 15:10

Doug Stevenson