I am aware that Compass will analyze the schema of a collection, but I am struggling to find a tool that will report that schema as a JSON file or preferably as a schema diagram. Is anyone aware of any tools that can achieve this?
Here is a tool for that: Variety, a Schema Analyzer for MongoDB.
you can use one of the MongoDB tools: mongodrdl is a relational schema management tool for the MongoDB Connector for BI. The
mongodrdl binary can:
Produce a schemabased on the contents of one or more MongoDB collections and write them out into
.drdlfiles used by
.drdlfile’s schema to the MongoDB deployment your BI Connector is reading from.
Downloadstored schemas from your MongoDB deployment.
Deletestored schemas from your MongoDB deployment.
Namestored schemas in your MongoDB deployment for easier management.
mongodrdl sample --db <db-name> --collection <collection-name> --out <filename>.drdl
The output is not json, what you are asking for, but since the drdl file is almost pure xml you can use one of the many online tools to convert xml to json.
@Tim_Busfield MongoDB Compass actually has a feature to “Share schema as JSON” but it is easily missed because you have to use a menu option at the moment:
- Choose a collection and select the Schema tab
- Click the “Analyze” button
- From the “Collection” menu choose “Share Schema as JSON” to copy the schema to the clipboard (or use the associated shortcut key, eg “Option (⌥) + Command (⌘) + S” on macOS).
- Paste from the clipboard into your favourite text editor
If you want to use the same schema analysis outside of Compass, it is open source (Apache 2.0 license) and usable as a Node.js library or
mongodb-schema command line tool: https://www.npmjs.com/package/mongodb-schema.
Thanks everyone for all of your suggestions, Stennie’s is the one that I used and I can only mark one as the “solution” but they were all helpful.