├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── browser.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '9' 4 | - '8' 5 | - '7' 6 | - '6' 7 | - '5' 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2017 Thomas Watson Steen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # json2mongo 2 | 3 | This is a [MongoDB Extended 4 | JSON](http://docs.mongodb.org/manual/reference/mongodb-extended-json/) 5 | convertion utility which converts Strict Mode syntax to JavaScript Mode. 6 | 7 | [![Build status](https://travis-ci.org/watson/json2mongo.svg?branch=master)](https://travis-ci.org/watson/json2mongo) 8 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) 9 | 10 | ## Installation 11 | 12 | ``` 13 | npm install json2mongo --save 14 | ``` 15 | 16 | ## Example usage 17 | 18 | ```javascript 19 | var json2mongo = require('json2mongo'); 20 | 21 | var query = { 22 | created: { $date: '2013-01-01T00:00:00.000Z' }, 23 | foo: { $undefined: true }, 24 | bar: { $regex: '[0-9]' }, 25 | baz: { $regex: '[a-z]', $options: 'i' } 26 | }; 27 | 28 | json2mongo(query); // { 29 | // created: new Date('2013-01-01T00:00:00.000Z'), 30 | // foo: undefined, 31 | // bar: /[0-9]/, 32 | // baz: /[a-z]/i 33 | // } 34 | ``` 35 | 36 | ## License 37 | 38 | MIT 39 | -------------------------------------------------------------------------------- /browser.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = function (obj) { 4 | var key, val 5 | for (key in obj) { 6 | if (!obj.hasOwnProperty(key)) continue 7 | val = obj[key] 8 | switch (key) { 9 | case '$date': 10 | return new Date(val) 11 | case '$regex': 12 | case '$options': 13 | return new RegExp(obj.$regex, obj.$options) 14 | case '$undefined': 15 | return undefined 16 | } 17 | if (typeof val === 'object') { 18 | obj[key] = module.exports(val) 19 | } 20 | } 21 | return obj 22 | } 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var mongo = require('mongodb') 4 | 5 | module.exports = function (obj) { 6 | var key, val 7 | for (key in obj) { 8 | if (!obj.hasOwnProperty(key)) continue 9 | val = obj[key] 10 | switch (key) { 11 | case '$binary': 12 | case '$type': 13 | return new mongo.Binary(obj.$binary, obj.$type) 14 | case '$date': 15 | return new Date(val) 16 | case '$decimal128': 17 | return new mongo.Decimal128(Buffer.from(val)) 18 | case '$timestamp': 19 | return new mongo.Timestamp(val.t, val.i) 20 | case '$regex': 21 | case '$options': 22 | return new RegExp(obj.$regex, obj.$options) 23 | case '$oid': 24 | return new mongo.ObjectID(val) 25 | case '$ref': 26 | case '$id': 27 | case '$db': 28 | var id = obj.$id._bsontype ? obj.$id : mongo.ObjectID(obj.$id.$oid) 29 | return new mongo.DBRef(obj.$ref, id, obj.$db) 30 | case '$undefined': 31 | return undefined 32 | case '$minKey': 33 | return new mongo.MinKey() 34 | case '$maxKey': 35 | return new mongo.MaxKey() 36 | case '$numberLong': 37 | if (typeof val === 'string') { 38 | return mongo.Long.fromString(val) 39 | } else { 40 | return mongo.Long.fromNumber(val) 41 | } 42 | } 43 | if (typeof val === 'object') { 44 | obj[key] = module.exports(val) 45 | } 46 | } 47 | return obj 48 | } 49 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json2mongo", 3 | "version": "2.0.0", 4 | "description": "A MongoDB Extended JSON conversion utility which converts Strict Mode syntax to JavaScript Mode", 5 | "main": "index.js", 6 | "browser": "browser.js", 7 | "scripts": { 8 | "test": "standard && node test.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/watson/json2mongo.git" 13 | }, 14 | "keywords": [ 15 | "mongo", 16 | "mongodb", 17 | "json", 18 | "convert", 19 | "transform" 20 | ], 21 | "author": "Thomas Watson Steen (https://twitter.com/wa7son)", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/watson/json2mongo/issues" 25 | }, 26 | "dependencies": { 27 | "mongodb": "^2.2.11" 28 | }, 29 | "devDependencies": { 30 | "standard": "^10.0.3" 31 | }, 32 | "homepage": "https://github.com/watson/json2mongo#readme", 33 | "engines": { 34 | "node": ">=5.10.0" 35 | }, 36 | "coordinates": [ 37 | 55.777582, 38 | 12.590236 39 | ] 40 | } 41 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var mongo = require('mongodb') 4 | var assert = require('assert') 5 | var json2mongo = require('./') 6 | 7 | var query = { 8 | _id: { $oid: '123456789012345678901234' }, 9 | created: { $date: '2013-01-01T00:00:00.000Z' }, 10 | decimal: { $decimal128: '42.42' }, 11 | ts: { $timestamp: { t: 1412180887, i: 1 } }, 12 | fkey1: { $ref: 'creators', $id: { $oid: '123456789012345678901234' }, $db: 'users' }, 13 | fkey2: { $ref: 'creators', $id: { $oid: '123456789012345678901234' } }, 14 | binary: { $binary: Buffer.from('foo') }, 15 | minKey: { $minKey: 1 }, 16 | maxKey: { $maxKey: 1 }, 17 | numberLong: { $numberLong: '9223372036854775807' }, 18 | foo: { $undefined: true }, 19 | bar: { $regex: '[0-9]' }, 20 | baz: { $regex: '[a-z]', $options: 'i' }, 21 | $and: [ 22 | { foo: { $undefined: true } }, 23 | { bar: { $undefined: true } } 24 | ], 25 | bool: true, 26 | obj: { foo: 123 }, 27 | string: 'foo' 28 | } 29 | 30 | var result = { 31 | _id: mongo.ObjectID(query._id.$oid), 32 | created: new Date('2013-01-01T00:00:00.000Z'), 33 | decimal: new mongo.Decimal128(Buffer.from('42.42')), 34 | ts: mongo.Timestamp(1412180887, 1), 35 | fkey1: new mongo.DBRef(query.fkey1.$ref, mongo.ObjectID(query.fkey1.$id.$oid), query.fkey1.$db), 36 | fkey2: new mongo.DBRef(query.fkey2.$ref, mongo.ObjectID(query.fkey2.$id.$oid)), 37 | binary: mongo.Binary(Buffer.from('foo')), 38 | minKey: mongo.MinKey(), 39 | maxKey: mongo.MaxKey(), 40 | numberLong: mongo.Long.MAX_VALUE, 41 | foo: undefined, 42 | bar: /[0-9]/, 43 | baz: /[a-z]/i, 44 | $and: [ 45 | { foo: undefined }, 46 | { bar: undefined } 47 | ], 48 | bool: true, 49 | obj: { foo: 123 }, 50 | string: 'foo' 51 | } 52 | 53 | assert.deepEqual(json2mongo(query), result) 54 | --------------------------------------------------------------------------------