[solved] Storing numbers as numbers, not strings


#1

I have documents stored in this format where ‘sort’ field defines sort order on the client:

{_id:...,sort:1},{_id:...,sort:2},{_id:...,sort:10},{_id:...,sort:20}

But meteor stores sort values as strings instead of numbers.
So if I query

Docs.find({},{sort:{sort:1}})

gives me this sort order: 1,10,2,20 - whereas I want it naturally to be 1,2,10,20.
How can I store numbers as numbers?

UPDATE:
Or maybe it’s just Meteor’s way of parsing this data?
When I connect to mongodb directly (using Robomongo) and query for

db.getCollection('docs').find({}).sort({sort:1})

it gives me correctly sorted output.


#2

Ok carefully auditing the db, I’ve found that I actually had mixed values in this field - strings and numbers! :stuck_out_tongue_closed_eyes:
Nutting wrong with Meteor here)
So if anybody runs into similar prob - make sure you store nums as nums in your db)


#3

The joy of dynamic typing and schema-less DB :wink:
Typescript to the rescue…