Skip to main content

NoSQL Databases - MongoDB first cut

A few days ago I tried MongoDB, my first encounter with what is called a NOSQL Database. No, it does not mean 'no' 'sql', rather Not only SQL, anyway I thought I would try mongodb, out of the many available there. It has both the document-oriented database design and ability to be accessed from Javascript, both features I really like. However, since I spend most time in Java, I thought I would start there.

First, let me tell you you should try their web console before writing a line of code or downloading anything because it's a really good self-run tutorial, that lets you type real commands and get real output all from the browser! pretty awesome...

Anyway, here is what I did to get started.
1. Download mongodb from http://www.mongodb.org/
2. Create working dir for MongoDB to use as data directory
3. Open favorite IDE or editor, and start writing test, yes Test, not source, in the spirit of Test Driven Development.
4. In test package, create test such as:

    @BeforeClass
    public static void setup() {
        db = new MongoDB(); db.connect(); // connect to mongodb
        conn = db.connectToDB("test"); // connect to a db called 'test', will be auto created
    }

    @AfterClass
    public static void close() {
        db.close();
    }

    @Before
    public void createdb() {
        coll = conn.getCollection("artifacts"); // fetch a collection called 'artifacts', will be autocreated
    }

    @After
    public void cleanup() {
        coll.drop(); // remove table
    }

    @Test
    public void testConn() {
        assertThat(conn.getName(), is("artifacts"));
        assertThat(conn.getCollectionNames().size(), is(0));
    }

Self explanatory, but shows how easy it is to get started with it.

Run this test, it will fail of course! Now on to the green of TDD...

Now, to make this test pass, we need to do two things : Create the objects we use in the tests, and start MongoDB database server.

First, in a source package, add the class MongoDB which would be like:

import com.mongodb.Mongo;
import com.mongodb.DB;

public class MongoDB {
    private Mongo m = null;

    public void connect() {
        try {
            m = new Mongo();
        } catch (Exception e) {
        }
    }

    public DB connectToDB(String name) {
        DB db = m.getDB(name);
        return db;
    }

    public void close() {
        m.close();
    }
}



Then start MongoDB as:
With a custom data directory (otherwise it assumes a weird location like /data), such as
   >     mongod --dbpath=/local/test/mongodb


Voila, run the test now and it should pass.

You noticed that I tested for the size of collections to be zero, not 1, because the collection 'artifacts' is not created until something is inserted into it.
Also, note that /local/test/mongodb acts as the data directory. for automated tests, you can create this dir in a temp dir, then delete it at the end of testing, which will require shutdown of mongodb. But we won't worry about that now.
Also inspect this directory as you run these tests.

To test this auto create feature of MongoDB, add a test to add a row to the collection, and see if you can get it back, the collection should be auto created.

@Test
    public void testMultipleEntries() {
        for (int i = 0; i < 10000; i++) {
            coll.insert(new BasicDBObject().append("i", i).append("j", i / 2)
                    .append("k", i * 2));
        }
        assertEquals(10000, coll.count());
    }


Pretty straightforward! The BasicDBObject is a Document object. In MongoDB a document is a record, much like a row is in a relational database. The insert() line basically inserts a document with fields 'i' 'j', 'k', into the collection 'artifacts', and with the use of builder pattern you can add as many append()s to keep inserting columns

So what are inserting in each loop iteration looks like:
{ "_id" : { "$oid" : "4dd076881368c78a6cfbdf96"} , "i" : 0 , "j" : 0 , "k" : 0}

If you want to see this formatted document entry, add something like
         System.out.print(coll.findOne());
which displays the first document record (or row) of the collection in JSON format

Comments

  1. I was at a MongoDB meetup the other day, there were a few topics on how people have used it forward various purposes.

    http://www.meetup.com/Boston-MongoDB-User-Group/events/25767451/

    One of the interesting ones for me was the geolocation support usage in combination with a Bing maps based application, as geospatial indexing is built in to MongoDB.

    Rather than me try to explain it, here is the link to the official doc:

    http://www.mongodb.org/display/DOCS/Geospatial+Indexing


    The other interesting talk was on rapid prototyping with MongoDb and a database, with practically nothing in between. While not suitable for production for security reasons, it looked quite useful to develop the GUI and the client API quickly without coding in the middleware business logic layer. I wonder how this will work when you do start coding the middle layer. Ingress the idea is that this is quick, and MongoDb's json abilities are ideally suited for web apps which are increasingly speaking this dialect.

    One more, in another post...

    ReplyDelete

Post a Comment

Popular posts from this blog

Authenticating Spring Boot based application against secure LDAP/AD server

Authenticating against an Active Directory setup is quite common in organizations using Spring Boot / Spring Security can be a pain if you don't know exactly the requirements. I needed to add auth in my web app and secure some but not all endpoints of the application. My story was, I needed Spring security to authenticate against my company LDAP server which uses Active Directory I started by using the standard LDAP guide such as this which are all over the Internet, https://spring.io/guides/gs/authenticating-ldap/ and was able to setup the basic framework However, only test level LDAP auth was working for me, when I tried to auth against the company LDAP secure server, I had to resolve a few issues After 1 week and working with several devs at the company, I finally found why it was not working and the fix was easy Since I spent a week or so resolving this, I wanted to write this up in case someone finds this useful. Here is what I did (it was easy until the fourth ...

Using custom conditional logic to enable/disable Spring components

If you have a Spring component and you don't want it to load, you can use Spring's predefined conditionals as much as possible. For example, @Component   @ConditionalOnNotWebApplication   public class SchedulerEntryPoint implements ApplicationRunner { ...  } This will not load your component when running in non web application mode. Such as you may want to start the application but without any of the web framework using SpringApplicationBuilder. But sometimes you want to use custom conditions. It's pretty easy to do so, just use something like this @Component @Conditional (SchedulerCheck. class ) public class SchedulerEntryPoint implements ApplicationRunner { public static class SchedulerCheck implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { return System. getProperty ( "scheduler" ) != ...

Sending Form data to a backend REST API using Axios

This need is incredibly common and useful, and hopefully will save you a lot of time when doing server side calls from your UI application (or even non UI clients like NodeJS applications) Example here is to send a POST request to an endoint /api/item/new (which will create a new item in the database). We will just assume tbhe backend is already setup (it's not relevant to this article). All we need to know is that we can do a POST /api/item/new and send it form data with two pieces of info     name, filter So, if you have a node.js application (I was using Vue-cli generated project, but it does not matter), install 'axios' (a most popular tool to make server calls these days) npm i axios --save OR yarn add axios (my preferred method) Now, in your service JS file (which is generally when I keep all my api calls) do something like this createNew ( name , filter ) { let formData = new FormData (); formData . append ( "name" , ...