I have a a collection named "users" within which sits a list of documents, each corresponding to a particular user. Within each of those user documents is a subcollection named "events" within which is a list of documents that have been created by parsing and processing several json files that the user uploads.
Schema: Users (collection) -> user(x) (document) -> Events (collection) -> event object documents.
An example json file (realistically, the files are much longer than the below example):
{
"events": [
{
"name": "name1",
"start": 1584165600,
"end": 1584180000
},
{
"name": "name2",
"start": 1583956800,
"end": 1583978400
},
{
"name": "name3",
"start": 1583550000,
"end": 1583978400
},
{
"name": "name4",
"start": 1578549600,
"end": 1583978400
}
]
}
So, in this case, there will be four documents within the "events" collection (each sub-object shown above).
I'm currently creating the above documents by using the add()
command for firestore.
The problem I'm facing is that I'm not sure how to implement the above so that every time the user uploads a new version of the file, the collection is overwritten with the new documents (so that there are no duplicates, which would otherwise happen with add()
).
A few notes:
- Because I'm using
add()
, the document IDs are random. - I can't use the name of each object as an identifier because some json documents that are uploaded have different structures.
set()
allows me to merge, yes, but I don't know how to merge documents without knowing their IDs.- There is the option of deleting the subcollection every single time a file is uploaded, but that just sounds like a lot of unnecessary work (and a lot of unnecessary processing)?
I'm open to suggestions of changing my implementation if there are any alternatives to using add()
as well in order to avoid the duplication issue :)
Edit: The above implementation will run from within a Cloud Function.