Create a gist now

Instantly share code, notes, and snippets.

Embed
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]);
}
}
}
@abt3bs

This comment has been minimized.

Show comment
Hide comment
@abt3bs

abt3bs May 22, 2015

works like a charm! Howver, I subsequently found the robomongo app and it's a nice tool to use in conjuction with mongo!

abt3bs commented May 22, 2015

works like a charm! Howver, I subsequently found the robomongo app and it's a nice tool to use in conjuction with mongo!

@abt3bs

This comment has been minimized.

Show comment
Hide comment
@abt3bs

abt3bs May 22, 2015

works like a charm! However, I subsequently found the robomongo app and it's a nice tool to use in conjuction with mongo!

abt3bs commented May 22, 2015

works like a charm! However, I subsequently found the robomongo app and it's a nice tool to use in conjuction with mongo!

@hkasera

This comment has been minimized.

Show comment
Hide comment
@hkasera

hkasera Sep 5, 2015

Thanks abt3bs! Glad it helped you 👍

Owner

hkasera commented Sep 5, 2015

Thanks abt3bs! Glad it helped you 👍

@Ankita-Jaiswal

This comment has been minimized.

Show comment
Hide comment
@Ankita-Jaiswal

Ankita-Jaiswal Apr 10, 2016

Thanks, it was really useful

Thanks, it was really useful

@pulkitsinghal

This comment has been minimized.

Show comment
Hide comment
@pulkitsinghal

pulkitsinghal Jun 11, 2016

For some strange reason, when running with MongoChef 3.5.1 IntelliShell, it errored out:

E QUERY    [thread1] ReferenceError: getKP is not defined :
@(shell):3:3

I had to move getKP() to be before the rest of the code but yes it worked, thanks!

For some strange reason, when running with MongoChef 3.5.1 IntelliShell, it errored out:

E QUERY    [thread1] ReferenceError: getKP is not defined :
@(shell):3:3

I had to move getKP() to be before the rest of the code but yes it worked, thanks!

@mvivekk

This comment has been minimized.

Show comment
Hide comment
@mvivekk

mvivekk Jun 8, 2017

Works cool.. Thanks :)

mvivekk commented Jun 8, 2017

Works cool.. Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment