/MongoDB_SpringBoot

Interacting with MongoDB local and MongoDB Atlas cloud using Spring Boot Framework

Primary LanguageJava

MongoDB_SpringBoot

Interacting with MongoDB local and MongoDB Atlas cloud using Spring Boot Framework

Preqequisites

  1. Download and install MongoDB in local machine
  2. Add environment variable for MongoDB bin folder
  3. Open command prompt and type mongod to start mongodb server. MongoDB server runs on port 27017
  4. By default mongod runs as a background service on windows
  5. Open another command prompt terminal and run mongo to start mongo shell client. It automatically connects to mongodb server on port 27017

#Cluster Mode - Replication

#create 3 mongo server cluster with 1 primary and 2 secondary

mongod --port 27017 --dbpath "C:\Users\keeth\Desktop\Dinesh-Spring-boot\mongoDB\mongo-replication\data0" --replSet "rs0" --bind_ip localhost
mongod --port 27018 --dbpath "C:\Users\keeth\Desktop\Dinesh-Spring-boot\mongoDB\mongo-replication\data1" --replSet "rs0" --bind_ip localhost
mongod --port 27019 --dbpath "C:\Users\keeth\Desktop\Dinesh-Spring-boot\mongoDB\mongo-replication\data2" --replSet "rs0" --bind_ip localhost

mongo shell client #this wil initiate the replica set and add 27017 to the replica set rs.initiate() #Add the other 2 nodes in the replica set rs.add("localhost:27018") rs.add("localhost:27019")

#Mongo DB Atlas

  1. Create a MongoDB Atlas cluster in free tier
  2. Obtain the cluster endpoint for shell and Java application
  3. Use the cluster endpoint to connect to the MongoDB Atlas Cluster either from shell or the application

#Steps

  1. Download the Spring Data MongoDB for the Spring Boot application to talk to the MongoDB database
  2. Use the MongoDB localhost credentials to connect to MongoDB local
  3. Use MongoDB Atlas cluster endpoint to access MongoDB in the cloud
  4. Run the java program
  5. Verify the data either from Mongo Atlas console or from the shell
  6. Use Spring boot mongotemplate to have more granual access to the data
  7. MongoRepository provides high level CRUD operations and we cannot control the data in more granular by queries
  8. MongoTemplate has query functionality which lets us to access only the data we require instead of loading all the unnecessary data

Mongo DB Architecture

MongoDB Architecture

MongoDB Atlas Console

MongoDB Atlas

MongoDB Stitch

MongoDB Stitch