Last active
October 4, 2018 07:48
-
-
Save vadym-vorobel/53ac11732ce4060ad2d367b143000381 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var structureTypes = { | |
type: 'type', | |
typeCount: 'typeCount', | |
}; | |
// ============ PARAMETERS ============= | |
var collectionName = ''; | |
var limit = 0; // 0 - analyze all documents | |
var structureType = structureTypes.type; | |
// ===================================== | |
var getValueType = (value) => { | |
if (value === null) { | |
return 'null'; | |
} | |
var valueType = typeof value; | |
if (valueType !== 'object') { | |
return valueType; | |
} | |
if (value instanceof Date) { | |
return 'date'; | |
} | |
}; | |
var buildTypeStructureForList = (documentsList, initStructure = {}) => { | |
var structure = Object.assign({}, initStructure); | |
documentsList.forEach((doc) => { | |
Object.keys(doc).forEach((key) => { | |
var types = structure[key]; | |
var value = doc[key]; | |
if (Array.isArray(value)) { | |
structure[key] = buildTypeStructureForList(value, types || {}); | |
} else { | |
var valueType = getValueType(value); | |
if (types) { | |
if (!types.includes(valueType)) { | |
structure[key] = `${structure[key]}, ${valueType}`; | |
} | |
} else { | |
structure[key] = valueType; | |
} | |
} | |
}); | |
}); | |
return structure; | |
}; | |
var buildTypeCountStructureForList = (documentsList, initStructure = {}) => { | |
var structure = Object.assign({}, initStructure); | |
documentsList.forEach((doc) => { | |
Object.keys(doc).forEach((key) => { | |
var types = structure[key]; | |
var value = doc[key]; | |
if (Array.isArray(value)) { | |
structure[key] = buildTypeCountStructureForList(value, types || {}); | |
} else { | |
var valueType = typeof value; | |
if (types) { | |
var typeCount = types[valueType] || 0; | |
structure[key][valueType] = typeCount + 1; | |
} else { | |
structure[key] = { [valueType]: 1 }; | |
} | |
} | |
}); | |
}); | |
return structure; | |
}; | |
var addCountRecord = (allDataCursor, structure) => { | |
return Object.assign({}, { '@allDocumentsCount@': allDataCursor.count() }, structure); | |
}; | |
var getCollectionStructure = (collectionName, limit = 0, structureType = structureTypes.type) => { | |
var cursor = db.getCollection(collectionName).find({}); | |
var limitedCursor = limit > 0 ? cursor.limit(limit) : cursor; | |
if (structureType === structureTypes.type) { | |
return addCountRecord( | |
cursor, | |
buildTypeStructureForList(limitedCursor) | |
); | |
} else if (structureType === structureTypes.typeCount) { | |
return addCountRecord( | |
cursor, | |
buildTypeCountStructureForList(limitedCursor) | |
); | |
} | |
return { | |
error: 'Structure type is invalid', | |
}; | |
}; | |
getCollectionStructure(collectionName, limit, structureType); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment