forked from watson/json2mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (39 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
var mongo = require('mongodb');
module.exports = function (obj) {
var key, val;
for (key in obj) {
if (!obj.hasOwnProperty(key)) continue;
val = obj[key];
switch (key) {
case '$binary':
case '$type':
// TODO: Will this behave if $type isn't set?
return new mongo.Binary(obj.$binary, obj.$type);
case '$date':
return new Date(val);
case '$timestamp':
return new mongo.Timestamp(val.t, val.i);
case '$regex':
case '$options':
return new RegExp(obj.$regex, obj.$options);
case '$oid':
return new mongo.ObjectID(val);
case '$ref':
case '$id':
// TODO: Does this follow the mongo.DBRef interface?
return new mongo.DBRef(obj.$ref, obj.$id);
case '$undefined':
return undefined;
case '$minKey':
return new mongo.MinKey();
case '$maxKey':
return new mongo.MaxKey();
case '$numberLong':
return new mongo.Long(val);
}
if (typeof val === 'object')
obj[key] = module.exports(val);
}
return obj;
};