Local Deployed(MAC)
1. install mongoDB by Docker
# open your terminal console
# install mongoDB
docker pull mongo
# --name : your mongoDB name
# username and password
docker run -d -p 27017:27017 --name smart_coach \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=123456 \
mongo
# check if the mongoDB is ready
docker ps
# if your reboot your computer, restart the image
docker start smart_coach
# send request through web browser, when you get the following picture
http://localhost:27017/
# content in the browers
It looks like you are trying to access MongoDB over HTTP on the native driver port.
2. connect through GUI (IntelliJ IDEA as example)
- click Test Connection to see if you can connect or not
3. create datasource for test
- jump to GUI console, execute below sql;
# create a new datasource,
# we need this name in the project applicsaiton.properties
use smart_coach;
# create sub user and password for this datasource
db.createUser({user:"sub_user",pwd:"123456",roles:[{role:"dbOwner",db:"smart_coach"}]})
# insert two rows into one collection(name: inventory)
db.testCollection.insertOne( {name: "danny", address: "us", age: 15 } );
db.testCollection.insertOne( {name: "tom", address: "uk", age: 20 } );
4. revise your application.properties file
## below need to change
## remember to change the host to 'localhost' if you deploy mongoDB in local
## username and password should be sub user we created above
spring.data.mongodb.database=smart_coach
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
spring.data.mongodb.username=sub_user
spring.data.mongodb.password=123456
## above need to change
# no change needed below
spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.jsp
spring.servlet.multipart.max-file-size=256MB
spring.servlet.multipart.max-request-size=256MB
spring.servlet.multipart.enabled=true
server.port=8081
spring.resources.chain.cache=false
spring.cache.type=none
5. test the database connection
- paste the code to your project
package com.ukrsoftprom.smartCoach.controller;
import com.mongodb.client.MongoDatabase;
import com.ukrsoftprom.smartCoach.dal.MatchDAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/test")
public class TestController {
@Autowired
private MatchDAL matchDAL;
@Autowired
private MongoTemplate mongoTemplate;
@RequestMapping(value = "/case", method = RequestMethod.GET)
public long testCase() {
MongoDatabase db = mongoTemplate.getDb();
return db.getCollection("testCollection").countDocuments();
}
}
- test the database connection through web browser
# result should be two
http://localhost:8081/test/case
6. LAST BUT NOT LEAST
- If you want to test your code, ALL mongo SQL related to this project should be executed in the console
- Because the mongoDB in local environment is empty right now
# chose the datasouce
use smart_coach;
# then you can execute other mongoDB sql
mongoDB sql in your project
Remote Deployed
to be continued…