Insert or update into a object with one query

Hi @Florian_Silbereisen,

So I noticed that you are using questions as an object and not array of objects. So my previous comment is irrelevant.

In that case a simple update will update or insert new fields:

db.users2.updateOne({"name" : "phil"},{$set : {"questions.nr1" : 1, "questions.question1" : "how am i?", "questions.answer1" : "bad"}})
{ acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0 }

{ _id: ObjectId("606d6cf0d81fef690531cedf"),
  name: 'phil',
  email: 'phil@example.com',
  questions: 
   { nr1: 1,
     question1: 'how am i?',
     answer1: 'bad',
     nr2: 2,
     question2: 'how are you?',
     answer2: 'good' } }

db.users2.updateOne({"name" : "phil"},{$set : {"questions.nr3" : 3, "questions.question3" : "how am i?", "questions.answer3" : "good"}})
{ acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0 }

db.users2.findOne()
{ _id: ObjectId("606d6cf0d81fef690531cedf"),
  name: 'phil',
  email: 'phil@example.com',
  questions: 
   { nr1: 1,
     question1: 'how am i?',
     answer1: 'bad',
     nr2: 2,
     question2: 'how are you?',
     answer2: 'good',
     answer3: 'good',
     nr3: 3,
     question3: 'how am i?' } }


Thanks,
Pavel