MCPcopy
hub / github.com/Pythagora-io/pythagora / mongoObjToJson

Function mongoObjToJson

src/helpers/mongodb.js:25–49  ·  view source on GitHub ↗
(originalObj)

Source from the content-addressed store, hash-verified

23// let methods = ['save','find', 'insert', 'update', 'delete', 'deleteOne', 'insertOne', 'updateOne', 'updateMany', 'deleteMany', 'replaceOne', 'replaceOne', 'remove', 'findOneAndUpdate', 'findOneAndReplace', 'findOneAndRemove', 'findOneAndDelete', 'findByIdAndUpdate', 'findByIdAndRemove', 'findByIdAndDelete', 'exists', 'estimatedDocumentCount', 'distinct', 'translateAliases', '$where', 'watch', 'validate', 'startSession', 'diffIndexes', 'syncIndexes', 'populate', 'listIndexes', 'insertMany', 'hydrate', 'findOne', 'findById', 'ensureIndexes', 'createIndexes', 'createCollection', 'create', 'countDocuments', 'count', 'bulkWrite', 'aggregate'];
24
25function mongoObjToJson(originalObj) {
26 let obj = _.clone(originalObj);
27 if (!obj) return obj;
28 else if (isObjectId(obj)) return `ObjectId("${obj.toString()}")`;
29 if (Array.isArray(obj)) return obj.map(d => {
30 return mongoObjToJson(d)
31 });
32 obj = convertToRegularObject(obj);
33
34 for (let key in obj) {
35 if (!obj[key]) continue;
36 if (isObjectId(obj[key])) {
37 // TODO label a key as ObjectId better (not through a string)
38 obj[key] = `ObjectId("${obj[key].toString()}")`;
39 } else if (obj[key] instanceof Date) {
40 // TODO label a key as Date better (not through a string)
41 obj[key] = `Date("${obj[key].toISOString()}")`;
42 } else if (obj[key] instanceof RegExp) {
43 obj[key] = `RegExp("${obj[key].toString()}")`;
44 } else if (typeof obj[key] === 'object') {
45 obj[key] = mongoObjToJson(obj[key]);
46 }
47 }
48 return obj;
49}
50
51function jsonObjToMongo(originalObj) {
52 let obj = _.clone(originalObj);

Callers 7

postHookFunction · 0.85
findAndCheckCapturedDataFunction · 0.85
getPopDataFunction · 0.85
getMongoDocsFunction · 0.85
configureMongoosePluginFunction · 0.85

Calls 2

isObjectIdFunction · 0.85
convertToRegularObjectFunction · 0.85

Tested by

no test coverage detected