MongoDB document oriented database | introduction

Mateusz Starzyk

Few words about document oriented databases

We live in the world where we are bombarded with huge amount of information, news and advertising. Cultural and social life, newspapers, TV are transferred to the network. Computer network becomes the central place of business. For this reason databases become fatter and fatter.

Computer systems are facing a challenge of storing a lot of different information and to ensure the maximum short time to access to them.  As we all know, in fact the customer/user cannot wait long.

Relational databases, which are with us over 40 years, on one side are very well optimized and tested but on the other hand – they stopped being enough to meet all the expectations of the software developers.

Scalability, performance and cloud computing are the key concepts which relational databases do not handle well with. The dispersion of data between multiple servers became very difficult problem.

Software developers needed also to face the challenge which is „change”. The world is changing very fast and at the same rate (or even faster) computer systems must also change. Agile methodology requires flexibility, making changes in easy way and speed of reaction. These factors are difficult or often even are not possible for achieving in relational databases, especially in case of very complex and complicated data structures.

In response to the needs of the market a new approach to the storage of data appeared: Document oriented databases. The data is stored in looser and natural way, without restrictions related to the scheme. The data is stored in the forms of „documents” which are independent data units.

This approach allows to separate data between servers without loss of efficiency, changes in the structure of the data are also very simple and access to data is very fast.

Of course such approach also have some disadvantages. The data structure is not verified in any way – for this reason keeping a unitary data structure is very heavy.  Because of that document oriented databases is very often used in case of unstructured data and for structured data the systems rely on relational databases. Another important limitation is transactional, which in case of document oriented databases can be maintained only at the level of the document or the entire database.

Data representation in MongoDB

In MongoDB data is stored in the form similar to JSON object named BSON. Documents are grouped into collections. We can say that document is a single row (compare to a relational database) and collection is a table.

Single document is represented by key-value pairs:

{
    _id: ObjectId(585bb162d348e719c87074f5)
    blog_article_title: 'MongoDB document oriented database - introduction',
    blog_article_description: 'Introduction to document oriented database',
    blog_article_author: 'MS',
    blog_article_seo_keywords: ['NoSQL', 'mongo', 'database', 'programing', 'MongoDB', 'fast database'],
    blog_article_comments: [
        {
            user:'John Doe',
            message: 'Nice job',
            created_at: '2016-12-23 10:00:00'
        },
        {
            user:'John Doe Junior',
            message: 'I like this post',
            created_at: '2016-12-23 11:00:00'
        }
    ],
    blog_article_created_at: '2016-12-23 10:00:00',
    blog_article_updated_at: '2016-12-23 10:00:00'
    blog_article_content: 'Some blog content'
}

Quick start with MongoDB using JAVA

In this part of the article I will show some basic operations (CRUD operations) on MongoDB using JAVA. To start we need to install MongoDB Community Edition. Installation manual depends on our operation system that we can find on MongoDB website: https://docs.mongodb.com/manual/installation/

If we want to use Java to work with MongoDB we need to add MongoDB JDBC driver to our program:

  • If we are using maven:
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-driver</artifactId>
    <version>{version}</version>
</dependency>

Now we are ready to start.

At the beginning we need to try to connect with our database server:

MongoClient mongoClient = new MongoClient();

This default constructor creates for use connection on default port 27017 with localhost url. If we changed MongoDB server settings we use one of different constructors from MongoClient class, for example:

MongoClient mongoClient = new MongoClient("localhost", 27017);

If we successfully connect to the server we can list all existing database:

MongoIterable<String> databases = mongoClient.listDatabaseNames();
for (String databaseName: databases) {
    System.out.println("Database name: "+databaseName);
}

No we can connect to the selected database:

MongoDatabase database = mongoClient.getDatabase("blog");

If selected database does not exist, mongo will create it for use.

C like Create

So now time to insert first data info our database. We can create our collection:

database.createCollection("blog_article");

This step is not really necessary, when we try to insert document into not existing collection, mongo will create collection for use. Now we can try to insert some data:

SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
MongoCollection<Document> blogArticleCol = database.getCollection("blog_article");
Document article1 = new Document();
article1.append("blog_article_title", "MongoDB document oriented database - introduction")
        .append("blog_article_description", "Introduction to document oriented database")
        .append("blog_article_author", "MSX")
        .append("blog_article_created_at", dt.format(new Date()))
        .append("blog_article_updated_at", dt.format(new Date()))
        .append("blog_article_content", "Some blog content")
        .append("blog_article_comments_count", 10);
blogArticleCol.insertOne(article1);

We have a few methods responsible for inserting data into database, except insertOne, we can use insertMany or bulkWrite

R like Read

If we have now some data in database we can try to read this data. To search documents in collection we are using method find():

FindIterable<Document> results = blogArticleCol.find();
for(Document row: results) {
    System.out.println(row.toJson());
}

If we use find method without any parameters, it will return all documents stored in the collection. We can also restrict number of result by adding search criteria:

  • using Filters helper:
blogArticleCol.find(and(eq("blog_article_author", "MS"), gte("blog_article_comments_count", 10)));

or filter Document:

Document filter = new Document("blog_article_author", "MS")
        .append("blog_article_comments_count",new Document("$gte", 10));
FindIterable<Document> results = blogArticleCol.find(filter);

U like Update

For document update we can use several methods like updateOne, updateMany or replaceOne.

First of them updates only one document even if there is more than one document that match the criteria:

blogArticleCol.updateOne(
        eq("blog_article_author", "MS"),
        combine(
                set("blog_article_comments_count", 11),
                currentDate("blog_article_updated_at")
        )
);

In case updateMany methods all of the documents that match the criteria will be updated.

We can also use replaceOne method. In this case object that match to the criteria will be replaced with a new document:

blogArticleCol.replaceOne(
        eq("blog_article_author", "MS"),
        new Document("blog_article_author", "John Doe")
);
 

D like Delete

The last operation from CRUD is Delete. For this operation we also have more than one possibility.  Similarly like in case of update, for delete we also have deleteOne (only one document will be removed, even if there is more than one document that match the criteria) and deleteMany (all documents that match the criteria will be removed from collection).

DeleteResult deleteResult = blogArticleCol.deleteMany(
        eq("blog_article_author", "MS")
);

System.out.println("Deleted documents: "+deleteResult.getDeletedCount());
 

Summary

I hope this article gave you a few important details about the document oriented databases and encouraged to the further explore possibilities MongoDB. In fact, there are much more possibilities than I was able to mention here.

Poznaj mageek of j‑labs i daj się zadziwić, jak może wyglądać praca z j‑People!

Skontaktuj się z nami