Find all field names in MongoDb

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

I am switching from PHP/Mysql to Clojure/MongoDb. In Clojure, hyphens in names are more idiomatic than underscores. So I wanted to find all the field names, so I could see where the underscores were. Having done a straight import of all of my database tables into a single MongoDb collection, I ran this Javascript at the Mongo shell to get all the field names.

var arrayOfFieldNames = [];

var items = db.tma.find();

while(items.hasNext()) {
item = items.next();
for(var index in item) {
arrayOfFieldNames[index] = index;
}
}

for (var index in arrayOfFieldNames) {
print(index);
}

Source