4

I am new to mongoDB and pymongo, trying to learn how to load and save databases from/to disk so that I can carry it around, to send it to people etc. I've read the tutorial on http://api.mongodb.org/python/current/tutorial.html but couldn't find useful information about saving and loading a db.

Assuming we create a database like:

import pymongo
mongo = pymongo.Connection()
db = mongo['my_db']
col = db['my_col']
col.insert({'name': 'Adam','occupation': 'student'})
col.insert({'name': 'John','occupation': 'officer'})
#how can we save the database to disk after this point 
#and later read it from another program?
9
  • read this at first api.mongodb.org/python/current/tutorial.html Commented Dec 21, 2012 at 10:40
  • As a matter of fact, I did, there is no such information for saving and loading db. Commented Dec 21, 2012 at 10:41
  • I don't understand what you asking. insert saves data to the database. If you are load "from/to disk" where do you expect it to load to? I think you may need to understand the fundamentals of databases here. Commented Dec 21, 2012 at 10:42
  • I mean when we want to use the database from another program, not necessarily from the same computer. Similar to sqlite's *.db files. Commented Dec 21, 2012 at 10:44
  • 1
    MongoDB comes with inbuilt server that transmits from your computer, if you wish to make the files portable (MongoDB is not an embedded database unlike SQLite) then you can copy your MongoDB directory normally located in /data/db/mongodb Commented Dec 21, 2012 at 10:56

2 Answers 2

9

MongoDB files are portable and there are a couple of ways to achieve what you are looking for:

  • Copy the data directory for mongod to another computer. This directory is normally based in /data/db/mongodb. On the other computer you would simply replace the remote directory with the one you copied and restart the remote mongod at which point you will have the data on their system. As far as I know MongoDB has no hot swap feature here whereby you can switch, on-demand, the directories without downtime.

  • Make hot backups of your data and use MongoDBs export and import functions to select subsets of data to place onto the remote machine. You would do hot backup via mongodump and mongorestore. You can find the general doc page on that here: http://docs.mongodb.org/manual/administration/backups/#using-binary-database-dumps-for-backups

  • Make CSV/JSON exports of your data and import it onto the remote computer. This is a lot like the binary backups in this particular scenario except they are also more readable to end parties etc. I should also note that this method only inserts, it is the same as a batch insert client side iterating (in something like PHP) a CSV and calling a batch insert on the MongoDB server. You can find more information about mongoexport and mongoimport (the programs that do this) here: http://docs.mongodb.org/manual/administration/import-export/

You can also find nice general information that relates to backing up data and moving it and restoring it on foreign machines here: http://docs.mongodb.org/manual/administration/backups/

Sign up to request clarification or add additional context in comments.

Comments

2
  • To insert a new entry in mongodb using pymongo what you did is sufficient. i.e. doing this

    col.insert({'name': 'John','occupation': 'officer'})

will save these entries automatically in the collection. You can also change the entries using pymongo like this:

col.update({'name': 'Adam'},{'$set':{'occupation': 'officer'}})

Also its a good practise to close connection at the end:

mongo.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.