Skip to content

Instantly share code, notes, and snippets.

@ryanbekabe
Forked from hkasera/mongo-struc.js
Created June 22, 2019 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbekabe/36c7fd1a01ad22e1542b8b71b981eac7 to your computer and use it in GitHub Desktop.
Save ryanbekabe/36c7fd1a01ad22e1542b8b71b981eac7 to your computer and use it in GitHub Desktop.
A script for mongo shell to get the collection structure.
var conn = new Mongo();
db = conn.getDB(<DB_NAME>);
var cursor = db.<COLLECTION>.find();
var items = [];
items = cursor.toArray();
var dbstruc = {};
for (var i = 0; i < items.length; ++i) {
var target = items[i];
getKP(target,dbstruc);
}
printjson(dbstruc);
function getKP (target,dbstruc) {
for (var k in target) {
if(typeof target[k] !== "object"){
if (target.hasOwnProperty(k) && !dbstruc.hasOwnProperty(k)) {
dbstruc[k] = typeof target[k];
}
}else{
dbstruc[k] = {};
getKP(target[k],dbstruc[k]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment