8

I am trying to add data to my firestore database. The firestore is this:

Firestore db

enter image description here

I have my php file that loads data into it with this code db.collection('events').doc('documentNameUsingUniqueEventID').set({id1:data1, id2:data2, id3:data3, ...});

but when I load new data and old still exists the documents gets overwrite and I lose the changes I made. Is there a way to add data avoiding overwrite existing ones?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Dennis
  • 372
  • 1
  • 2
  • 17
  • Do you always want to create a new document with a new ID? If so, use `.add`, instead of `.set` (don't include the `.doc()` reference) If you want to specify the document ID, you can either `.set` the new document and `.update` subsequent writes. If you don't know if the ID that you are using exists or not, then Doug's method of `.set(data, {merge: true})` should work best for you. – Jason Berryman Feb 05 '18 at 18:55
  • When I call the php file I always use .set because I get json from a txt file. The problem is that in the json I have as ID the unique event ID, not the autogenerated by firestore so when I call php it overwrites all data even if I use merge:true.. I just want to keep participants and ageAverage whenever the php gets called – Dennis Feb 05 '18 at 19:25
  • `.set(data, {merge: true})` _should_ do exactly what you're trying to do. However, I'm not familiar with the PHP SDK. There could be some inconsistency here. You would need to reach out to one of the Firebasers with some PHP SDK knowledge. Sorry! – Jason Berryman Feb 05 '18 at 22:38
  • {merge: true} doesn't overwrite data which are not send, but I always send participants: 0 and ageAverage: 0. Do you have any advice of how can I solve my problem? – Dennis Feb 07 '18 at 07:19

4 Answers4

18

The set() method on DocumentReference has a second parameter for SetOptions that allows you to say that you want to merge data into an existing document rather than overwrite it:

db.collection('events')
    .doc('documentNameUsingUniqueEventID')
    .set(data, { merge: true });
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I have already tried but it doesn't work. I update partecipants and ageAverage from an android application and via php I upload new events from a sever. In php data parameters I have partecipants and ageAverage set at 0, when I do the .set() with merge:true it still overwrite them to 0 – Dennis Feb 05 '18 at 16:34
  • 1
    @Doug Stevenson exits any way to insert some values if not exits and update (with merge) others if exits? in the same statement? Like a SQL merge – IoT user Dec 14 '18 at 10:06
  • 1
    for me it is now: .set(user, SetOptions(merge: true)); – Dannark Aug 26 '20 at 04:56
7

Use .add on a collection to add data with an Auto ID.

Use .set on a document to completely overwrite it.

Use .update on a document to update parts of it.

You also spelled participants wrong.

vonUbisch
  • 1,384
  • 17
  • 32
1

Now you can enable merge in set by SetOptions.merge() for example

db.collection('events')
    .doc('documentNameUsingUniqueEventID')
    .set(data, SetOptions.merge());
Shibin Raju Mathew
  • 920
  • 4
  • 18
  • 41
0

I solved my problem using .update() and {merge: true}. I don't pass participants and ageAverage in php but I create them when I have to upload data

Dennis
  • 372
  • 1
  • 2
  • 17