Spring MongoOperations to access MongoDB

MongoDB is an open-source NoSQL document database, written using C++. This tutorial will introduce way to use Spring Boot to access MongoDB and perform CRUD operations with Spring MongoOperations.

Related Articles:
How to build SpringBoot MongoDb RestfulApi
How to use SpringData MongoRepository to interact with MongDB
SpringData MongoDB GridFsTemplate to save, retrieve, delete binary files (Image, Text files)
Angular 4 + Spring Boot + MongoDB CRUD example

I. Technology for Spring MongoOperations tutorial

– Java : 1.8
– Maven: 3.3.9
– Editor: Spring Tool Suite – Version: 3.7.3.RELEASE.
– Spring Boot: 1.4.0.RELEASE

II. Overview
1. Structure of project

spring data mongodb mongooperations structure project

2. Step to do

– Create Spring Boot project
– Define MongoDb config beans
– Define Document model
– Implement MongoDB Crud operations
– Run & Check Results

III. Practice
1. Create Spring Boot Project

– Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info. Then press Next.

Now add MongoDB dependency by go to NoSQL, select MongoDB:
MongoOperations dependencies

Press Finish, Spring Boot Project will be created successful.

Open pom.xml file, check dependecy:

2. Define MongoDb config beans

MongoDbFactory: an interface for factories creating DB instances.
MongoOperations: an interface that specifies a basic set of MongoDB operations.

3. Define Document model

@Document: identifies a domain object to persisted to MongoDB
@Id: demarcates an identifier.

4. Implement MongoDB Crud operations

Save a document

Search a document

Update a document

Find all documents

Delete a Document:

5. Run & Check Results

spring data mongodb

IV. Source code


By grokonez | October 30, 2016.

Last updated on November 28, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *