Realm: creating a new user document

I’m attempting this with the following code:

exports = async function(authEvent) {
  const mongodb = context.services.get("mongodb-atlas");
  const users = mongodb.db("sportrank").collection("users");

  const { user, time } = authEvent;
  const newUser = { ...user, eventLog: [ { "created": time } ] };
  
   await users.updateOne({ id: newUser.id },
   { $set:
      {
        "custom_data.active": true,
        "custom_data.description":{level: '', comment: '' },
        "custom_data.alternate_emai": "",
        "custom_data.nickname": "",
        "custom_data.ownerOf": [{}],
        "custom_data.memberOf": [ {}]
      }
   }
  )
  await users.insertOne(newUser);
}

I have created an authentication trigger that runs this code on authentication.

A new user is created via the client app, but the custom_data fields are not set.

Is there another/better approach (another trigger?)?

How should I set these fields to default (empty) values on a new user creation? thanks …

I had the update before the insert. Once corrected it worked:

exports = async function(authEvent) {
  const mongodb = context.services.get("mongodb-atlas");
  const users = mongodb.db("<my_db>").collection("<user_collection>");

  const { user, time } = authEvent;
  const newUser = { ...user, eventLog: [ { "created": time } ] };
  
  await users.insertOne(newUser);
  await users.updateOne({ id: newUser.id },
   { $set:
      {
        "custom_data.active": true,
        "custom_data.description":{level: '', comment: '' },
        "custom_data.alternate_email": "",
        "custom_data.nickname": "",
        "custom_data.ownerOf": [{}],
        "custom_data.memberOf": [ {}]
      }
   }
  )
}