Mongoose create not adding reference field to collection #15031
Labels
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
needs clarification
This issue doesn't have enough information to be actionable. Close after 14 days of inactivity
Prerequisites
Mongoose version
8.7.3
Node.js version
22.5
MongoDB version
7
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
11
Issue
I'm working on a project and I created a model that reference another model(collection) using the _id of that other model in my current model. To my shock, when I create a new document, the document I created will be inserted into the collection successfully but MongoDB wouldn't add/display the referenced field in the MongoDB collection and this makes the populate() not to be working, infact I couldn't find the referenced field in the Customers collection but other fields records(fullname, phone, email) i created are there.
Here are my codes:
-------baseCustomer.model.js
-------customer.model.js
------Controller user controller.
************WHAT I EXPECTED
a) I expected to see the baseCustomer_id in the Customers collection after I created record but I found just only the fullname, phone, email in the collection without the baseCustomer_id field. So, with the current challenge I cannot use something like populate() to get the customerType and role fields from the baseCustomer collection because there is no baseCustomer_id field in the Customers collection in MongoDB.
I want to be able to count Customers based on their customerType e.g Total Number of Bronze Customer, Total Number of Gold Customer, and Total Number of Platinum Customer in just one query using either aggregate or populate from mongoose nodejs.
Thank you for your assistance in advance.
The text was updated successfully, but these errors were encountered: