So i have a few days trying to debug my restful API, specifically mongoose and MongoDB on prod server (aws server). There is a service which query Users collection and returns the model, simple enough. But the question is this why would this query criteria return an empty [] on prod server but returns the model on localhost and using Postman for testing purposes.On prod server i will have to be explicit on what data i want from the document in order to return the model. Below i will leave my codebase such as the schema, services.
NOTE -> my prod server and local machine are both Ubuntu. Prod server (Ubuntu 18.04.3, codename Bionic) and local machine (Ubuntu 21.10, codename Impish).
Schema
userSchema: {
name: {
type:Schema.Types.String,
required: true
},
carsOwned: [{
name: {
type: Schema.Types.String,
},
brand: [{
name: {
type: Schema.Types.String,
},
}]
}],
totalNumberOfCarsOwned: {
type: Schema.Types.Number,
default: function () {
let carsCount = 0;
for (let car of this.carsOwned) {
carsCount += car.brand.length;
}
return carsCount
}
}
Sevice
userService.getCarById = (id) => {
// this is the original query which works perfectly locally but not on live server (aws)
return userModel.findOne({carsOwned: {$elemMatch: {_id: id}}})
// This is the updated query which works perfectly both locally and live server (aws)
return userModel.findOne({carsOwned: {$elemMatch: {_id: id} } }, {"carsOwned._id":1, "carsOwned.brand":1 });
// noticed how i exclude totalNumberOfCarsOwned in the updated query criteria. If i included the property totalNumberOfCarsOwned then it returns an empty array like user = []
};
Both versions of my MongoDB (5.0.8) and Mongoose(5.4.16) are the same locally and on prod server. I simply don't understand why others query works fine and this is the only service that has a tricky and weird temporary solution.
I just need an guide that points me to an answer that explain this issue, please.