0

When I try to add a field to an object, instead of adding the field to the object it deletes all of the previous fields and adds the new field.

data.collection.updateOne({},{$set: {"object": {"hi":0}}},true)

What I want:

object: {
      hello: 0,
      hi: 0,
}

What I get:

object: {
      hi: 0,
} 

I tried everything I could but I did not get it to work as I wanted it to.

Kevin M. Mansour
  • 2,915
  • 6
  • 18
  • 35
  • Check this link it may help https://stackoverflow.com/questions/55596504/how-to-insert-new-object-without-deleting-previous-one – Husain Ahmmed Feb 01 '21 at 18:21

1 Answers1

0

Here it is:

 db.we.updateOne( { },{$set:{"object.hi":0 } } , true)
 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
R2D2
  • 9,410
  • 2
  • 12
  • 28