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.

import java.net.UnknownHostException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.mongodb.MongoClient;
public class AppConfig {
    public MongoDbFactory mongoDbFactory() throws UnknownHostException{
        return new SimpleMongoDbFactory(new MongoClient("localhost", 27017), "test");
    public MongoOperations mongoOperations() throws UnknownHostException{
        return new MongoTemplate(mongoDbFactory());

3. Define Document model

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

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
public class SimpleDocument {
    String id;
    String name;
    String description;
    public SimpleDocument(String name, String description){
        this.name = name;
        this.description = description;
    public String toString() {
        return String.format("[id = %s, name = %s, description = %s]", id, name, description);

4. Implement MongoDB Crud operations

Save a document

SimpleDocument simpleDocument = new SimpleDocument("document_1", "this is description for document 1");
// save

Search a document

// build mongo query
Query query = new Query(Criteria.where("name").is("document_1"));
// search operation
SimpleDocument object = (SimpleDocument) mongoOperations.findOne(query, SimpleDocument.class);

Update a document

//update operation
 mongoOperations.updateFirst(query, Update.update("description", "update description"), SimpleDocument.class);

Find all documents

// find all simple document in DB
 List objLst = mongoOperations.findAll(SimpleDocument.class);

Delete a Document:

// delete a simple document in Db
mongoOperations.remove(query, SimpleDocument.class);

5. Run & Check Results

spring data mongodb

IV. Source code


By grokonez | October 30, 2016.

Last updated on April 10, 2021.

Related Posts

2 thoughts on “Spring MongoOperations to access MongoDB”

  1. You don’t talk about the most important and hardest to get good examples for use case. Aggregations to pull out just what you need without loading an entire collection into a List

Got Something To Say:

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