If you have always wondered what is Mongo or NoSQL Databases and would like to experiment with it, yes you have come to the right place. So lets get on with it instead of the so called introduction/TOC/contents boilerplates!
What is Mongo?
Mongo is a NoSql DB in fact a document database which is distributed, scalable and easy to query with the ability to even do transactions! Obviously its loved by millions and is very easy to integrate with loads of languages and tools. But let’s get a level deeper shall we?
NoSQL –
Basically they are schema less DB’s unlike traditional relational DB’s or RDBMS like Oracle and Sql Server or Postgres and MySql.Schema less means you don’t need a pre-defined structure or data modelling like the scripts you need when you setup tables in postgres .So does this mean you can just dump any json like structure , images or any resources in a noSQL DB? YES! Isn’t it simple and easy to develop?
Different kinds of NoSql Db’s
- Document databases – store data in documents like JSON /YAML/XML.Eg Mongo, Elastic Search,CouchDB,DocumentDB
- Key-value databases – each record has a key and a value like a dictionary or hash table.Most of your caches work on this model.Eg Hazelcast, Redis, Memcache
- Wide-column stores – data in stored in tables, rows, and dynamic columns.Mostly used in Big Data processing. Cassandra, Hbase, Google’s Bigtable
- Graph databases – data in stored in nodes and edges.Popularly used in social media apps, when you have a person and their connections and many other related info .
Pro’s and Con’s of NoSQL DB’s
Its basically great for quick uploads and fast retrievals of data with the ability to horizontally scale.Can store different models without constraints. Cons are obviously data duplication as these are not normalized and are not meant for transaction based apps which need ACID compliance.
Mongo
Since we now know what kind of NoSQL DB Mongo is , you would know that docs or unstructured data(Eg your customer object can just be saved directly instead of ER modelling and entity -model mapping) can be stored and retrieved pretty fast using mongo.But in addition you should know that there is always the capability to do indexing to improved reads and aggregation like group by in RDBMS.
Cut to the chase!
There are several quick ways to get started. Popular among them is to use Atlas DB from here. They give you some basic storage space in their cloud offering under community license which is enough to play around with. Else you can also spin one off a docker image. Once you have your mongo url you can go ahead and connect it using either a IDE or CLI tools as well. Since you are beginning , i would suggest go with an IDE, popular ones include the Atlas but i liked NoSQLBooster for Mongo DB and Studio 3T, these are free with some limited features, but more than enough for day to day development.
Basics
How do you relate to the sql world when i venture out in Mongo? Well to start off with you need a Database and if you don’t create, Mongo will create one for you . And the tables in Mongo are called Collections and each collection has lots of documents under them which all have a unique identifier object called _id. All the individual properties of the document are called fields (corresponding more like a column in sql) . Thats pretty much everything you need to know.
eg if you need to access a certain DB called “testDB” just query
use testDB
Then you would want to create a document under a collection called testingMongoDocs
testDB.testingMongoDocs.insertOne( { hello: “World” } )
Congratulations you have created your first document!! But wait i forgot to create the collection .Dont worry Mongo has taken care of it for you!
Here are some basic queries which i think might help you, mostly CRUD.
//Fetch first 100 rows in desc order
db.sampleCollection.find({}).projection({}).sort({_id:-1}).limit(100)
//Projection helps in display and sort with 1 is asc and -1 is desc
//COUNT
db.sampleCollection.find().count()
//DISTINCT
db.sampleCollection.distinct("emp_role")
//DELETE
db.sampleCollection.remove({status: "Ex"})
// AND Condition
db.sampleCollection.find({$and:[{emp_id:'0986'},{status:'Active'}]})
//Update many
db.sampleCollection.updateMany({$and:[{emp_id:'1042'},{status:'Active'}]},{$set:{"status":"Ex"}})
//In command when you have many values
db.sampleCollection.find({$and: [{dept_id:"1041"},{proj_id:{$in: ["1233","1336","1346"]}},{is_active: true}]})
Since you are truly on your way to becoming a pro at Mongo now that you have the basics under belt, probably you would want a lil challenging query as well? how about aggregation?
Here is a sample
db.sampleCollection.aggregate({$match: { $and: [{role: "Mgr"},{is_active:true}]}},
{$group: {_id: "$dept_id",
count: {$sum: 1}}},
{$match: { count: { $gt: 1 }}}
)
Now that you are familiar with most of the queries you would now want to dabble with Spring boot and wire your code.
Spring Boot Integration
Now coming to development using Spring Boot , as always there is a Spring jpa will help you connect with the Mongo. You will need to add the following in you maven or gradle build file.Example given for gradle.
implementation ‘org.springframework.boot:spring-boot-starter-data-mongodb’
Once we have added the lib, we can add a MongoRepository to a api, which is basically a Hibernate implementation of Spring Data JPA.Something like this.
@Repository
public interface CartRepository extends MongoRepository<CartActivity, String> {
CartActivity getCartActivitiesByKey(String key);
}
As you can see you can extend the MongoRepository in your repository layer/DAO and its takes the Document you want to return and the key field type for the Document. But where is the Document ?Well you have to create one in your project for example here is a sample.
@Document(collection="cart_products")
public class CartActivity {
@Indexed
@Field(value = "key")
private String key;
@Field(value = "activity_timestamp")
private Instant activityTimestamp;
@Field(value = "value")
private String value;
}
There are a few terms you might now be aware.@Document annotation indicates that it is a Mongo Document and take a collection name as param. Also @Indexed here indicates that this is going to indexed, because we will most likely query based on this field , so to improve perf we can add @Indexed to fields that we would want to index. Finally each property on the object has to be tagged with @Field to indicate that they will need to be saved in the Document .
Now coming to the final piece of the puzzle , you would want to add the application yaml or properties file with all the config values which will help you connect to the MongoDB .A sample is here below.
spring:
data:
mongodb:
uri: mongodb://user:password@mongo_instance:27017/DB_name?authSource=role
jpa:
show-sql: true
logging:
level:
org:
springframework:
data:
mongodb:
repository:
query: debug
If you have replica sets for your MongoDB you will need to have a little different uri(check here). What i have provided above is a standalone instance. The relevant param i have mentioned will need to be replaced with relevant values. authSource is only when you want to set access control and restrict to the roles that one can access. And whats with all the logging and other jpa attrb? Well i have added this to the config , if you want to see the exact query that is performed behind all the magic that Spring JPA and Hibernate does behind the scenes.
Resources