This error is occurring because the schema for the office field in your companySchema has not been registered with the mongoose model. In your companySchema, you have set the ref field of the office property to “Branch.office”. This is not the correct way to reference the office field in the branchSchema. Instead, you should reference the Branch model and its office field as follows:
This will tell Mongoose to populate the office field with the data from the Branch model.
To read more about it refer to the Mongoose documentation link.
Also, make sure that you have imported the Branch model into the file where your Company model is defined. You can do this by adding the following line at the top of your file:
i know of that way but that returns the big array of offices stored in branch i don’t want the entire array of offices i am trying to get to a single office by referencing that branch _id Branch.office is there no way to do this? what if i have 1000 office stored in the array of offices—>
Here office is a field as per your branch model which contains an array of sub-documents.
The ref option is what tells Mongoose which model to use during population. So, the model is branch which will be used to populate the office field. To read more refer to this link.
If you want to populate the sub-document from the large array set by referencing that branch _id, you can do as following: