0

I'm trying to create a one to many relationship using Node JS and Mongoose. I have the following:

  • an object for a company (with all the details about it and also the owner of this company. First, the owner will be an ID, given using a POST request).
  • an object for the owner (found by the ID given in the first step)

What I want to introduce in the database:

I want to reuse the object for the company, and change the owner id with the owner object, and then to introduce it in the database. The problem is that when I want to change the value for the owner, it remains the same.

This is the company object:

var newCompany = new Company({
            owner: req.body.owner,
            name: req.body.name,
            email: req.body.email,
            phone: req.body.phone,
            web: req.body.web
        })

I have this:

const companyOwner = await User.findById(newCompany.owner)
newCompany.owner = companyOwner

So whant can I do to finish this task?

Thank you!

Catalin A.
  • 19
  • 5
  • use the `save()` function. `newCompany.save()` or if you need to wait for it to return `await newCompany.save()`. – Andre Aug 05 '20 at 17:47
  • 3
    Does this answer your question? [Mongoose: Find, modify, save](https://stackoverflow.com/questions/14199529/mongoose-find-modify-save) – Andre Aug 05 '20 at 17:48
  • The save() function saves the object to the database, right? I want to change the content of the object first, and then to save it. In this point, my newCompany object have the id of the owner at the property newCompany.owner. I want it to have the object of the owner. You understand? Thank you for helping me. – Catalin A. Aug 05 '20 at 18:34

1 Answers1

0

Following up on your comment:

To answer your question, you have to understand the concept of foreign keys and ObjectIds in mongoose / MongoDB.

Most likely, your Company schema contains an ObjectId reference to the User schema:

owner: { type: Schema.Types.ObjectId, ref: 'User' }

This means, you are storing the user._id inside your company object to point to an user object. Mongoose will make sure that it only stores the _id and not the whole object to reduce storage costs. Mongoose is smart enough that if it detects a user object like company.owner = companyOwner; it will actually do this: company.owner = companyOwner._id;. This is where your confusion is coming from.

You want to have the user object, probably to pass it on or return by your endpoint and this is possible but you have to do it differently.

For storing: Just keep your code and mongoose will make sure that only the user._id is stored.

For retrieval: populate() is what you are looking for. After you saved your company to the database, you can retrieve it including the full populated user object by calling:

const company = await Company.findById(company._id).populate('owner');

This will return the company object with a populated user object. So you only store the user._id but mongoDB and mongoose will make sure to give you the corresponding user object.

Andre
  • 4,185
  • 5
  • 22
  • 32