Export schema of a Collection

1,475 views
Skip to first unread message

Senthil Kumar

unread,
May 1, 2014, 10:51:11 AM5/1/14
to mongod...@googlegroups.com
Hi,

I have large collections in MongoDB and would like to export the structure of the Collection alone into a file(csv/excel,ec) for Documentation purpose.

I can export the field names with data but couldn't export/copy the types of fields into a file.

Thanks,
Senthil

s.molinari

unread,
May 2, 2014, 8:34:00 AM5/2/14
to mongod...@googlegroups.com
Wouldn't you have the schema (object) definitions somewhere in your programming?

Scott

Stephen Steneker

unread,
May 2, 2014, 2:44:29 PM5/2/14
to mongod...@googlegroups.com
On Friday, 2 May 2014 00:51:11 UTC+10, Senthil Kumar wrote:
I have large collections in MongoDB and would like to export the structure of the Collection alone into a file(csv/excel,ec) for Documentation purpose.

I can export the field names with data but couldn't export/copy the types of fields into a file.

Hi Senthil,

As Scott noted, a more typical source of schema for documentation would be your application code. That said, it may still be useful to infer the schema of an existing collection.

Since MongoDB has dynamic schema, there are several caveats to discovering the overall structure of a collection:
 - there is no server-side "schema" to enforce or query for a collection so you have to inspect the documents
 - fields present can vary per document within a collection
 - fields of the same name can have different types within a collection
 - you potentially have to inspect all documents in a collection to infer the overall schema

There are not (as at MongoDB 2.6) any commands or tools included to infer a schema, however there are definitely third party tools for this.

A handy one I'm aware of is Schema.js, which is an add-in for the `mongo` shell that infers the schema in JavaScript via Map/Reduce: http://skratchdot.com/projects/mongodb-schema/. By default it looks at 50 documents, and you can adjust the limit to more (or all) documents. The output can be inline or to a collection.

Regards,
Stephen

Reply all
Reply to author
Forward
0 new messages