0
votes

The firebase documentation says this about updating in nested objects: https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects

My structure

let ref = db.collections('projects').doc('blabla')
ref.set({
    objOfObjects: {}
})

I would like to add an object(value) with a randomly generated key to objOfObjects, much like the regular add function. I can't tie the key to any of the values of the object. Is there a way to add to objOfObjects or do I have to restructure my data?

1
post the firebase structure that you wants to update or any screenshotHimanshu

1 Answers

1
votes

There is no built-in function add a field with a randomly generated name to a document. But you can generate the random name yourself and add it. You can even use collection.add() to generate the random name, since (as Doug says here) that doesn't actually create a document yet.

var newId = store.collection('projects').doc().id;

let ref = db.collections('projects').doc('blabla');
var updates = {};
updates[id] = { ... }
ref.update(updates)

The tricks used here:

  • Get a new unique id, as explained before.
  • Use [] notation to use the value of that id as the property name.
  • Call update() to update a document, instead of replacing it.