We are making progress.
I understand but if one part of mongoose (data insertion using the schema) does type conversion it would make sense that the other part (data retrieval using the schema) does the same data conversion.
const testSchema = new mongoose.Schema(
{
field : { type: String }
}
test = { field : 5 } /* 5 the number, not the string */
Test.insertOne( test )
/* Document is stored as { "field" : "5" } 5 the string because of type
conversion */
/* In the following findOne you really want result to hold the Document
inserted. Do you? I think you do and it would not make sense if you
did not. */
result = Test.findOne( test )
/* If result is a document with field:"5", the string,
then type conversion is done. */
I did not imply that either was wrong when I asked you to share your schema, documents and code. I think it could help comprehension because we could use your real schema and document rather than the one I made up above with approximate syntax.
The SO post I shared seems to indicate the contrary.
We are making progress because we went from
to
It makes sense that mongoose would not do type conversion in $match. In a lot of $match, there is no schema related to the $match-ed documents. In a pipeline, documents are altered or even generated (by $group). It would take a lot of complicated code to determine which schema to use, if any, to do type conversion. And it will be very confusing. For 1 $match you have conversion because we have a schema and for another one there is none because it is generated document.
My conclusion is
1 - type conversion occurs (may be not in your case) when documents are inserted because of the schema
Personally, a warning or error would be better.
2 - type conversion occurs in simple queries where it is clear which schema is used
It makes sense because conversion occurs at insertion.
3 - type conversion does not occurs in pipeline for $match because it would be hard to do and it will be very confusion because it cannot be done on some of the $match
4 - storing numbers as string is a bad idea
A string with a couple of digits will take more space than the number and it is slower to compare. Numbers as strings are not in the natural sort order “2” > “111” but 2 < 111.
5 - I am sure about the following.
Hopefully, mongoose has a way for you to communicate and ease your frustration.