Just another site

Tag Archives: MongoDB

MongoDB Driver Import Issue – Resolved

Tonight my younger daughter had a orchestra concert at her school.  I went and sat on the gym bench reading “Hadoop: The Definitive Guide”.  Even though I cannot do some hands-on exercises, it gets me started to see the actual Map/Reduce Java code.

After the concert I continue to work on my first Java MongoDB program.  Since the issue was that the interpreter cannot find the MongoDB Java driver, I re-downloaded mongo-java-driver-2.11.3.jar file and place it in the /usr/lib/mongodb folder (created by me).  I am not sure if it’s the right folder but I gave it a try.  Some blogger suggested to put it in the JAVA_HOME folder.  That led me to discover that I didn’t have the variable set.  After some searching and I’ve found that I have multiple versions (6 and 7) of openjre jar files in /user/lib/jvm  folder.  So, I set that variable in my .bashrc file for future use.

I’ve found in the forum (, someone has a similar question and this is the answer that worked:

In project explorer right click on the folder/package where you want to import the JAR. Then choose “Import”. In the import wizard, select General > File system. Browse for the JAR and select it.Now you imported the JAR, but before you can use it, you need to add it to the classpath. Once again in project explorer right click on the project root folder, select Properties. In the properties menu select Java Build Path. In Java Build Path open the libraries tab and click on “Add JARs…” and browse for the JAR you just imported.

If you do not wish to actually import the package to your project but just use it, then you can skip the first part of this instruction and directly go to the “Libraries” tab in the Java Build Path. This time you should click on “Add external JARs…” and browse for the JAR in your file system.

I imported the jar file by selecting the Import option after right click on the project icom in Package Explorer, then select General/File system and browse to the folder.  After the selection, the list boxes on the right showed the jar files but I check the folder on the left list box.  I guess it means importing all the jar files in that folder.  

I didn’t set classpath but it worked.  I’ve added some code to add documents to the collection and iterate through the documents in the collection to print out the objects.

I like how the driver support  DBObject that serializing into JSON format.


BasicDBObject doc = new BasicDBObject("name", "MongoDB").
                              append("type", "database").
                              append("count", 1).
                              append("info", new BasicDBObject("x", 203).append("y", 102));


    for (DBObject obj : col.find()) {

The result is:

{ “_id” : { “$oid” : “52b146b8e4b04530a89967c2”} , “name” : “John” , “type” : “Singer” , “lastname” : “Lennon” , “info” : { “x” : 203 , “y” : 102}}

I guess they might have the same thing for C# and I just haven’t came across it.  So far I’ve only tried the Linq driver, which does require you to define a POCO class to represent it’s schema.


Installing mongoDB on Ubuntu

Tonight I’ve done some learning on Java programming.  Once I got used to the syntax and keywords, it’s quite easy.  I can do interface, inheritance (extends).  I also found a way to add additional option for JRE version by changing the property of the project.  The use of binary value (0b10101, for example) only works for JRE 7.  Eclipse automatically asked me if I want to switch to 7.  Nice.

Then, I proceed to install mongoDB on my Ubuntu system.  The official mongoDB site does it one way, without storing key for mongodb user.  I’ve found this site provides a different way to do the server installation.

Now I can start and stop the server.  However, I still need to find out how to import the mongoDB driver JAR files into my project.  

Last Week’s Learning on C# programming on MongoDB

Over the week, I had done the following:

1. On Windows, played with MongoDB C# and the Linq adaptor.  The sample tutorial (  shows that you have to declare a POCO class that expose one particular property:

public class Entity
    public ObjectId Id { get; set; }

    public string Name { get; set; }

Then, after created a 

var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("test"); // "test" is the name of the database
// "entities" is the name of the collection var collection = database.GetCollection<Entity>("entities");

After that, you can insert a new document by using collection.Insert, query the collection by 

var query = Query<Entity>.EQ(e => e.<field name>, <field value to Search>);
var entity = collection.FindOne(query);

Or, use Set or Save methods to modify the document.

However, I feel that there must be another sets of APIs that allows type-less access to the database. The beauty of NoSQL is that it allows schema-less design.  I saw that some of the methods has a different type of interface so perhaps that’s what it’s for.  Will explore that later.

2.  I’ve installed Ubuntu on my retired Windows PC and put Java and Hadoop on it.  This time, I used the Cloudera CDH package so things went a lot smoother.  Everything uses the default install and there’s no need to modify the site.xml files.  These are all very new to me so I am not 100% understanding what went on.  But, the WordCount sample code compile and run.

I’ve created 2 shell script files in my ~/bin folder.  hadoop-hdfs-start and hadoop-hdfs-stop for starting and stopping all the processes for hadoop, mapred, and yarn.

Read more of this post

Started to learn MongoDB

I temporarily put aside my quest on Hadoop and dabbled in MongoDB a little this week, just because I wanted something less frustrating.

MongoDB is relatively easy to get into.   You download the file, unzip it, created a directory dedicated as the data folder and another for the log folder.  You can setup a mongod.cfg file for basic configuration for the MongoD.exe, the services engine listening on the port.

MongoD runs as a Windows service.  To install it, open a cmd prompt window, run as Administrator and type

  • Bin\mongod.exe –config c:\learn\mongoDb\mongod.cfg –install

The config file needs to be in absolute path.  If not, it needs to be set that way in the registry.

Open a command line prompt.  Run Mongo.  That’s the client.  It present you with a console to write your command.  The code is in Java.  Will find out whether it supports C#.

Default to test as database.

  • db for show the current opened database.
  • Dbs shows all the databases
  • Use <database> to create and open a new database

Instead of table, MongiDb has collections.

  •  db.<collection>.insert(<something>)

Generate data in a loop

  • for (var i = 1; i <= 25; i++) db.testData.insert( { x : i } )

Show data with

  • db.testData.find()

It will show all the objects in testData.  It does by pages.  Need to type “it” to go to another page

That’s it so far.