Forcing Javascript numbers to Double in MongoDB document?

I want to save an arbitrary Javascript Object to a MongoDB Document, but I want all the numbers to be type Double in the database. All the fields which aren't arrays or objects will be strings or numbers.

For example:

{ "things": [ 1.0, 1.1, [ { "item": 6 }, 5.5, "test" ] ],
  "other things": { "thing": 2.0, "other thing" : [ 4.4, 5.0 ] },
  "another thing": 6.0 }
}

The nodejs-mongodb-native driver saves numbers which it can (e.g. 5.0) as Int32.

I can force the numbers to be doubles by replacing them with Double objects:

var mongo = require('mongodb');
new mongo.Double(number);

Is there a better way to accomplish this than recursively walking through the entire Javascript object replacing each number with a mongo.Double object?

You don't have to replace the variables with mongo.Double throughout your code. It's sufficient to do so when you create the document you store in your database. Using an object-relational mapping framework could also help you with that.

When you want to store your Javascript objects as they are in the database and don't have an intermediate database-version of the objects, you could create a wrapper function to save objects to MongoDB which creates a deep-copy of the object, but stores all integers in the source document in mongo.Double's in the target document. The target document is then saved to the database.