Doing the impossible – Embedding an mongoDB document that has not yet been created..?


This Content is from Stack Overflow. Question asked by Lukas Doughty

I’m creating a data base of user accounts with embedded profile documents so I can store the data separately but in a way where they are still linked. I want to include a the ID of the user account within the profile document in order to reference the user account in my app without passing the user account info to the client (eg email and password).

The problem is that for the user ID to be generated the user document needs to be created, but if I create the user document first then I cant link the profile to it as it has not yet been created. If I try to tackle this from the other way round then I cant add the userID to the profile as the ID will not yet have been created…

    const newProfile = new Profile ({
      companyName: companyName,
      eventGenre: eventGenre,
      area: area,
      established: established,
      description: pDescription,
      userID: //(this should be the ID of the user docuemnt created below)

     const newUser = new User ({
       email: email,
       password: password,
       remeberMe: 0,
       accountType: 1,
       promoterProfile: newProfile,
       venueProfile: null


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?