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.
Hello Tim,
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 schema
based on the contents of one or more MongoDB collections and write them out into.drdl
files used bymongosqld
. -
Upload
a.drdl
file’s schema to the MongoDB deployment your BI Connector is reading from. -
Download
stored schemas from your MongoDB deployment. -
Delete
stored schemas from your MongoDB deployment. -
Name
stored schemas in your MongoDB deployment for easier management.
usage:
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.
Regards,
Michael
@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: mongodb-schema - npm.
Regards,
Stennie
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.