I am trying to find a SoldProduct with intimeOrderNumber equal to '3'.
var query = { intimeOrderNumber: '3' };
But I get an empty array.
SoldProduct.find(query, function(err, soldProducts) {
if (err) {
console.log(err);
} else {
console.log(soldProducts.length);
for (var i = 0; i < soldProducts.length; i++) {
if (soldProducts[i].intimeOrderNumber === '3') {
console.log(typeof soldProducts[i].intimeOrderNumber);
console.log(soldProducts[i].intimeOrderNumber);
}
}
}
});
Output:
0
But when I change the query like this:
var query = {};
and run the same find, it's evident that there actually is a SoldProduct with intimeOrderNumber equal to '3' in the database because the output changes to:
15
string
3
I have intimeOrderNumber: String in the SoldProduct model definition. Why does the first query return an empty array?
The problem was in the fact that I had been adding the intimeOrderNumbers to the DB directly through the MongoDB console and I accidentally added Numbers instead of Strings.
So when looking for the document, '3' did not match 3, so it didn't find it. But when I found it with that empty query, the intimeOrderNumber somehow got converted to a String because that's the way it was set in the model.