Open OlammieAO opened 2 weeks ago
Can you please confirm that baseCustomer_id
is actually set in your req.body
? That would be one explanation for why that field isn't showing up in MongoDB.
Also, can you please confirm what the difference is between AppCustomer
model and Customer
model?
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.