If you haven’t used mongoDB before, its worthwhile trying it out first using the online shell available from the mongoDB website at
Step 1 – Install mongoDB
Download and install a copy of mongoDB from
http://www.mongodb.org/downloads
Installation is pretty straightforward, simply unzip the contents of the downloaded zipfile into a directory. I’m using the 64-bit version of mongoDB on a Windows 7 64-bit machine. It’s also worth noting that MongoDB is self-contained and does not have any other system dependencies. It may be installed in any directory (e.g. D:\test\mongodb) and can be run from any folder.
A number of command line programs are provided, for this example, assume that c:/mongodb contains the contents of the zip file.
To start the mongoDB database process, in my case, I executed the following command in a Win7 Command Prompt
C:\DATA\WORKPLACE\mongoDB\bin\mongod.exe --dbpath C:\DATA\WORKPLACE\mongoDB\data\
after a short time you will see the messages similar to
Fri Jun 07 16:58:33.353 [websvr] admin web console waiting for connections on port 28017 Fri Jun 07 16:58:33.353 [initandlisten] waiting for connections on port 27017
and you can start the mongoDB shell in another Command Prompt window
c:\DATA\WORKPLACE\mongoDB\bin\mongo.exe
and you have mongoDB up and running
MongoDB shell version: 2.4.4 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > db.test.save( {a:1} ) > db.test.find(); { "_id" : ObjectId("51b11b409d5b5279e8adbedb"), "a" : 1 }
Its also possible to setup mongoDB as a Windows Service, so that the database will start automatically following each reboot cycle – see the mongoDB installation instructions for further details.
Step 2 Transferring the Landscape database from MySQL to mongoDB
One method is to first create a CSV format version of the MySQL table then use the utility mongoimport, which is provided, and enables you to import a CSV file into mongoDB. To obtain a description of all the available options simply use the –help option to mongoimport
c:\DATA\WORKPLACE\mongoDB\bin\mongoimport --help
There are 3 tables in the Landscape MySQL database, so I exported each table as a CSV file and imported each one in turn.
c:\DATA\WORKPLACE\mongoDB\bin\mongoimport -d landscape -type csv -f entry_id,url,name,description,date -collection entries -file links.csv connected to: 127.0.0.1 Fri Jun 07 02:07:02.168 check 9 100 Fri Jun 07 02:07:02.170 imported 100 objects
This command line
creates (or adds to) a database called “landscape”
defines the input file to be of type “csv”
defines the incoming 5 fields as “entry_id”, “url”, “name” “description” and “date”
creates a collection called “entries” to contain the imported records
ans specifies the input file as “links.csv”
Simlar commads are used to import the other tables
c:\DATA\WORKPLACE\mongoDB\bin\mongoimport -d landscape -type csv -f entry_id,category_id -collection entry2cat -file entry2cat.csv c:\DATA\WORKPLACE\mongoDB\bin\mongoimport -d landscape -type csv -f category_id,category_name,category_desc -collection category -file category.csv
Other Information
I also came across a mongoDB GUI tool called MongoVue which I have found useful. It is available in a number of paid-for configurations and a free (limited) version is available too. MongoVUE is a desktop application for Windows that gives you an easy to use GUI interface when working with mongoDB.
With mongoDB running, create a connection in MongoVue and connect to the database. Here’s the one I set up for our Landscape project
Here’s how the entries collection looks in the mongoDB version of the Landscape database when examined wih MongoVUE
MongoVue appears to be a capable and useful tool. I notice that it also provides an option to import directly from a live MySQL database, although I didn’t try it for this project.
I also note a number of other available functions that I may examine in more detail later.
Next Step
Now that we have our data available in a mongoDB form, the next stage is to examine how we can access the data in a way that is suitable for a user to add and view the data from a web browser.
previous: NoSQL | next: tbd |