Interacting with MongoDB running in local and also Mongo Atlas cloud using Java API
Preqequisites
- Download and install MongoDB in local machine
- Add environment variable for MongoDB bin folder
- Open command prompt and type mongod to start mongodb server. MongoDB server runs on port 27017
- By default mongod runs as a background service on windows
- 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
- Create a MongoDB Atlas cluster in free tier
- Obtain the cluster endpoint for shell and Java application
- Use the cluster endpoint to connect to the MongoDB Atlas Cluster either from shell or the application
#Steps
- Download the MongoDB driver for the application to talk to the MongoDB database
- Use the MongoDB localhost credentials to connect to MongoDB local
- Use MongoDB Atlas cluster endpoint to access MongoDB in the cloud
- Run the java program
- Verify the data either from Mongo Atlas console or from the shell
Mongo DB Architecture
MongoDB Atlas Console
MongoDB Stitch