0

Being a newbie to mongodb, my apologies if this is a trivial question, hopefully not.

I've got a set of 10 json files generated on a mongodb db, for which I would like to investigate the structure (fields, names, sizes, etc.). I know how to import json files into a db/collection on my mongo system and then use dbfind. However this works for specific fields which you need to know in advance, right?

In a relational SQL DB I would simply import the files and then go through the field names, sizes, even export it to a standard file format (.CSV, etc).

  • When you know nothing about your source files, what is the equivalent strategy for MongoDB?
  • Is there a command that lists all the fields/elements in the doc?
  • Can you export the DB/collection from mongo, to a different format that is more manageable for discovery (e.g. .csv, or a SQL database).

Thanks in advance, p

user3310782
  • 811
  • 2
  • 10
  • 18

1 Answers1

1

In order to list all the fields in a mongo collection use variety https://github.com/variety/variety

OR

mr = db.runCommand({
  "mapreduce" : "my_collection",
  "map" : function() {
    for (var key in this) { emit(key, null); }
  },
  "reduce" : function(key, stuff) { return null; }, 
  "out": "my_collection" + "_keys"
})

Then run distinct on the resulting collection so as to find all the keys:

db[mr.result].distinct("_id")
["foo", "bar", "baz", "_id", ...]

MongoDB Get names of all keys in collection

FOR MONGO EXPORT

https://docs.mongodb.com/manual/reference/program/mongoexport/

For mongo export in csv format, you have to mention the fields you want to export, which you can get from above.

mongoexport --db users --collection contacts --type=csv --fields name,address --out /opt/backups/contacts.csv
Community
  • 1
  • 1
Nishant
  • 3,614
  • 1
  • 20
  • 26
  • Tried, then had to stop. I had some problems probably 'cause of my inexperience. This is what I do: 1I import the json file using cmd into test db, collection example -OK 2) I go to mongo shell. Run your mapreduce approach. I get error {"ok":0, "errmsg": "ns does not exist"} I haven't tried variety or export yet. – user3310782 Jul 04 '16 at 11:12
  • Just yours: mr = db.runCommand({ "mapreduce" : "example", "map" : function() { for (var key in this) { emit(key, null); } }, "reduce" : function(key, stuff) { return null; }, "out": "example" + "_keys" }) , of course not sure of meaning or the like. – user3310782 Jul 04 '16 at 11:52
  • dude, it means, ns doesn't exist means that you're accessing a non-existent database or collection. Check their names – Nishant Jul 04 '16 at 12:06