添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
耍酷的墨镜  ·  Spring Data MongoDB ...·  1 月前    · 
讲道义的鞭炮  ·  springboot mongodb ...·  1 月前    · 
有情有义的橙子  ·  birdben·  1 月前    · 
爱听歌的咖啡豆  ·  Harnessing·  3 周前    · 

Tutorial

Spring Boot MongoDB

Published on August 3, 2022
author

Shubham

Spring Boot MongoDB

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Welcome to Spring Boot MongoDB example. Spring Boot is the easiest way to spin a spring project quickly and MongoDB is the most popular NoSQL database. Let’s see how to integrate spring with MongoDB database.

Spring Boot MongoDB

Spring Initializr tool for quickly setting up the project. So, let’s get started.

Spring Boot MongoDB Project Setup

Maven Dependencies

Spring Boot from the maven central.

Spring Boot MongoDB Model Class

Spring Boot MongoDB APIs

Spring Data MongoDB - MongoRepository

Defining MongoDB properties

Defining the Spring Controller

Autowired the repository interface dependency and we will use this next.

Defining the APIs

Spring Data MongoDB - MongoTemplate

Spring Data MongoDB Test Run

MongoTemplate vs MongoRepository

Spring Boot MongoDB Summary

Download Spring Boot MongoDB Example Project

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Shubham

author

Still looking for an answer?

Ask a question Search for more help

Was this helpful?
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
October 28, 2020

Nice Tutorials for learning MongoDB along with other Java based technology. As per above code, for adding the New User, Mapping methods should be Post, here it mentioned Get. Kindly rectify me, If I am wrong… ################################################ Adding a new User setting Let’s try to add some data to an existing user: @RequestMapping (value = “/settings/{userId}/{key}/{value}”, method = RequestMethod.GET) public String addUserSetting( @PathVariable String userId, @PathVariable String key, @PathVariable String value) { User user = userRepository.findOne(userId); if (user != null) { user.getUserSettings().put(key, value); userRepository.save(user); return “Key added”; } else { return “User not found.”; } }

- prashant sinha

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 20, 2020

HOW TO CHECK MONGODB CONNECTION STATUS IN JAVA AND THROW CONNECTION ERROR

- CHETAN

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 28, 2020

Hi, little more info: A) DOCKER MONGODB: ==================== GIT DOCKER: https://hub.docker.com/\_/mongo PULL MONGO: docker pull mongo RUN DOCKER MONGO WITH NO USER+PSW: docker run -d --name mongo-on-docker-no-user-psw -p 27017:27017 mongo RUN DOCKER MONGO WITH USER+PSW: docker run -d --name mongo-on-docker-user-psw -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret mongo USER=mongoadmin + PSW=secret MONGODB CLENT APP MongoDB Compass (MongoDB 4.2.8 Community): ============================================================= https://www.mongodb.com/try/download/compass CONNECTIONS: MONGODB WITH NO USER+PSW: mongodb://localhost:27017/journaldev MONGODB WITH USER+PSW: mongodb://mongoadmin:secret @localhost :27017/journaldev

- VLADIMIR

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
June 27, 2020

I am getting below error when running the application. Can someone help! 20-06-27 22:44:46.903 INFO 9680 — [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017 com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.3.jar:na] at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.3.jar:na] at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.3.jar:na] at java.base/java.lang.Thread.run(Unknown Source) [na:na] Caused by: java.net.ConnectException: Connection refused: connect at java.base/java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:na] at java.base/java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:na] at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:na] at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:na] at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:na] at java.base/java.net.PlainSocketImpl.connect(Unknown Source) ~[na:na] at java.base/java.net.SocksSocketImpl.connect(Unknown Source) ~[na:na] at java.base/java.net.Socket.connect(Unknown Source) ~[na:na] at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.3.jar:na] at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.3.jar:na] … 3 common frames omitted

- Sharath Raj

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
March 12, 2020

Thank you so much!

- Anjali

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 18, 2020

@EnableMongoRepositories is required in order to autowiring works.

- Reza

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 7, 2020

hello, does we need connector for that ?

- joe

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
November 13, 2019

where to put mongodb configs?