12

I can take mongodb data backup but I am not sure about mongodb schama backup. Is there any way to take dump of MONGODB schema only not the data ?

sakshi
  • 171
  • 1
  • 1
  • 14

4 Answers4

3

You need to use mongorestore... which is used for things like importing json, or csv, etc.

You can read more about mongorestore in the docs below; I'd take a look and read up on them as they are very helpful.

http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongorestore

You can also check out http://learnmongo.com for tips and help! or you can visit the links

How to use the dumped data by mongodump? hope this may be helpful for you.

Community
  • 1
  • 1
Shekhar Tyagi
  • 1,644
  • 13
  • 18
  • Didn't see any dump/export schema options in the MongoDB link. Did find one item of interest, "mongodump only captures the documents in the database". If mongodump doesn't capture the schema, how can mongorestore restore them? – JohnC Nov 22 '17 at 17:30
  • yeah the question was about schema info, not data. even though mongo is schemaless the question is about how to get some of the structure from the DB. even the first record would be valuable if you're using something like Mongoose – dcsan Jun 30 '20 at 03:16
3

MongoDB is an NoSQL Database.

There is no fixed schema for any collection, so there are no functions available in mongo shell to find the collection schema.

Fixed Schema is applicable for RDBMS databases. In NoSQL DB, such as mongodb it is not required, but you can enforce same schema using your implementation logic, if required.

A document in a same collection, can be of different schema's. Please see example below

db.mycollection.insert([ 
{ "_id":1, "name":"A"}, 
{ "_id":2, "name":"CD", "age":29}, 
{ "_id":3, "name":"AB", "age":28}, 
{ "_id":4, "name":"ABC", "age":27, "emailId":"abc@xyz.com"}, 
{ "_id":5, "name":"ABCD", "age":29, "emailId":"abcd@xyz.com"}]);

db.mycollection.find();

{ "_id" : 1, "name" : "A" }
{ "_id" : 2, "name" : "CD", "age" : 29 }
{ "_id" : 3, "name" : "AB", "age" : 28 }
{ "_id" : 4, "name" : "ABC", "age" : 27, "emailId" : "abc@xyz.com" }
{ "_id" : 5, "name" : "ABCD", "age" : 29, "emailId" : "abcd@xyz.com" }

An approach to find the schema

In Mongo Shell
var k = db.mycollection.findOne();
for ( i in k){print (i)};
  _id
  name

this approach will work for you if all the documents in your collection follows the same schema.

Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34
  • 3
    Even though MongoDB is schema-less, its nice to have a script to setup the sharding and indexing. – JohnC Nov 22 '17 at 17:20
  • 1
    MongoDB now supports schema validators + definitely it's a valid point to want to export and perhaps version control all indices and similar schema-related settings aside from data. – Vojtěch Vít Sep 10 '19 at 10:25
  • this is neat but it doesn't do nested structures, just the top level. in 2020 is there a better way? – dcsan Jun 30 '20 at 03:16
  • @dcsan - you can analyze and export a schema using Compass https://docs.mongodb.com/compass/current/schema/export – StingyJack Jan 27 '21 at 14:50
1

Here's how I did it:

mongodump --uri="mongodb://localhost/mydb" -o ./mydb-dump
find ./mydb-dump -name *.bson -exec truncate -s 0 {} \;

Explanation: I'm dumping the whole database, then truncating all the .bson files (which hold collection data) to zero bytes.

Limitation: Obviously, this is only practical if the source database is small, otherwise you're generating a huge data dump only to throw away most of it.

To restore this-

mongorestore --uri="mongodb://some-other-server/mydb" ./mydb-dump

If there's a better way to do this, I'd love to know what it is!

Kip
  • 107,154
  • 87
  • 232
  • 265
  • Had to do `for /r %i in (*.bson) do copy /Y nul: %i` instead of your second line. However upon using mongorestore, it didn't work. Gave the error `don't know what to do with file` for each file. – Pepperized May 14 '20 at 11:43
  • I got it working by dumping collection to `./dump/db` using a uri ending in`/db` and then restoring using `./dump/` without specifying a db in the uri. It did not work when specifying a db for the restore in uri. unclear as to why. – Pepperized May 14 '20 at 11:50
0

MongoDB Compass GUI has a way to export the schema to JSON.

At the time of this post, there doesn't seem to be a way to do this by bulk, so this will have to be done for each collection one by one.

From the docs:

You can export your schema after analyzing it. This is useful for sharing your schema and comparing schemas across collections.

If you have not already done so, analyze your schema:

Select your desired collection and click the Schema tab. Click Analyze Schema.

Once your schema has been analyzed, export your schema:

In the top menu bar, click Collection. From the dropdown, click Share Schema as JSON.

enter image description here

Your schema is copied to your clipboard as a JSON object.

See full docs here ~ https://www.mongodb.com/docs/compass/master/schema/export/

TrieuNomad
  • 1,651
  • 1
  • 21
  • 24