0

I have a case where if there is data in my Firestore database, I want it to fail on create a new document, and definitely not overwrite or send any updates to that document.

I'm doing these in batches so I can't have any batches fail or it kills the whole batch.

Here's what I've tried/ruled out:

batch.update - Ruled out due to updating data batch.set - Ruled out as its default is to overwrite the data batch.set...{merge: true} - Ruled out as it would overwrite any fields I already have batch.create - This seemed hopeful but fails when a document already exists

let batch = db.batch()
...
batch.xyz(refToCreateOnly, Data) // this should be successful either way such that it does not kill the batch, and only updates if the ref does not exist
...
await batch.commit()

Is the best/possibly only way to do this with a transaction?

Thingamajig
  • 4,107
  • 7
  • 33
  • 61

1 Answers1

0

Yes, your only option here is to use a transaction to verify that the document doesn't exist prior to adding it.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441