1

I want to add new Property to an Object already exists in Cloud Firestore

/*
After Update 1, my Expectation:
  MyObject = {
     'NewProperty1': 'newValue1'
  }
*/
db.collection('users').doc(id).update({
  MyObject: {'NewProperty1': 'newValue1'}
});


/*
After Update 2, my Expectation:
  MyObject = {
     'NewProperty1': 'newValue1',
     'NewProperty2': 'newValue2',
  }
*/
db.collection('users').doc(id).update({
  MyObject: {'NewProperty2': 'newValue2'}
});

/*
After Update 3, my Expectation:
  MyObject = {
     'NewProperty1': 'newValue1',
     'NewProperty2': 'newValue2',
     'NewProperty3': 'newValue3',
  }
*/
db.collection('users').doc(id).update({
  MyObject: {'NewProperty3': 'newValue3'}
});

Here as you can see I just add new property to one object.

Right now this code every time overwrite the new property with old property.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Muhammad
  • 2,572
  • 4
  • 24
  • 46

2 Answers2

7

The following should do the trick

db.collection('users').doc(id).update({
  'MyObject.NewProperty1': 'newValue1'
});

Se the doc for more details.


If the property names are dynamic (refer to your comments below), you can do as follows:

  var key = 'NewPropertyX';
  var value = 'propertyX';


  db.collection('users').doc(id).update({
    [`MyObject.${key}`]: value
  });
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Thank you, but the property is something dynamic, for example in every update there is random id and I use that Id as property with a true or false value. – Muhammad Jan 18 '20 at 13:16
  • What exact part is dynamic? MyObject or NewProperty1? – Renaud Tarnec Jan 18 '20 at 13:45
  • MyObject is always exists and is one Object, and can have many properties, like NewProperty1, NewProperty2, NewProperty3, NewPropertyX. So How to add new property1, property2 and propertyX ... to MyObject which is a field for my `users` collection and `user` doc. – Muhammad Jan 19 '20 at 11:44
  • 1
    See the update. – Renaud Tarnec Jan 19 '20 at 13:16
  • Thank you so much, I will test it and then will check it as solved. And please let me know how money property is possible for one object. Can I have 1000 to 5000 property-value asigned to One object in firestore? – Muhammad Jan 19 '20 at 13:40
  • 1
    Glad I could help you! You will find in the [doc](https://firebase.google.com/docs/firestore/quotas#collections_documents_and_fields) more details on Firestore limits. In your case, I think the following may apply: Maximum size of a field value: 1 MiB - 89 bytes (1,048,487 bytes) ; Maximum depth of fields in a map or array: 20 – Renaud Tarnec Jan 19 '20 at 13:43
1
use set method

    db.collection('users').doc(id).set({
     'NewProperty2': 'newValue2',
    });
  • set without merge will overwrite a document or create it if it doesn't exist yet

  • set with merge will update fields in the document or create it if it doesn't exists

  • update will update fields but will fail if the document doesn't exist

  • create will create the document but fail if the document already exists

See this answer:https://stackoverflow.com/a/46600599/1680793

honor
  • 7,378
  • 10
  • 48
  • 76
himeshp
  • 353
  • 4
  • 12