mongoify

Converts an object with native JavaScript types and objects to MongoDB Extended JSON object

mongoify

Converts:

{
  foo: { bar: 1 },
  date: new Date('2014-01-01T00:00:00Z'),
  regex: /foo/i,
  bla: undefined
}

To this:

{
  foo: { bar: 1 },
  date: { $date: '2014-01-01T00:00:00Z' },
  regex: { $regex: 'foo', $options: 'i' },
  bla: { $undefined: true }
}

This is called MongoDB Extended JSON.

So that you can JSON.stringify the object for easy storage or transport.

npm install mongoify
var mongoify = require('mongoify');
 
var obj = { date: new Date('2014-01-01T00:00:00Z') };
 
mongoify(obj);
 
console.log(JSON.stringify(obj));

MIT