Data Type issue with mongo - suggestions on avoiding this pitfall?

I have a collection where a field (chartStatus) is numeric, and I like to do a filter on that numeric field:

 return Chart.find({"chartStatus": filterType}, {sort: {"demographics.lastName": 1}});

The problem arises when I forget to convert filterType to a numeric with parseInt.

For example, I’m using iron router, and taking a parameter from the url, like:


which would pass the 1 down to the find method but in this case, the “1” comes in as a string, and it doesn’t find any records.

This was tricky to debug, because doing console.log("filter coming in is " + filterType) shows the same output regardless of whether filterType is a string or a number.

Anyone have a suggestion on how to avoid this?

Well first of all that happens because the number is converted to a string as you’re concatenating it to a string. It’s a lot easier to see with an object for example, as you’ll get something like [object Object] instead.

I would think your options are to do as you do and convert it to a number from a string or to store your chartStatus value as a string instead.