how to save list,set,map in mongodb using springboot

how to save list,set,map in mongodb using springboot

directory structure

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringBootMongodb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootMongodb</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.properties



spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=newdb

spring.jpa.hibernate.ddl-auto=update

SpringBootMongodbApplication

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootMongodbApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMongodbApplication.class, args);
    }

}

Employee.java

package com.example.demo.entity;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import lombok.AllArgsConstructor;
import lombok.Data;

@Document(collection = "emp_info")
@Data
@AllArgsConstructor
public class Employee {    
    @Id
    private Integer id;    
    private String name;    
    private String[] projects;    
    private List<String> companies;    
    private Set<Long> contactNo;    
    private Map<String,String> cardDetails;
}

EmployeeRepo.java

package com.example.demo.repo;

import org.springframework.data.mongodb.repository.MongoRepository;

import com.example.demo.entity.Employee;

public interface EmployeeRepo extends MongoRepository<Employee, Integer>{

}

EmployeeMgmtService.java

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.entity.Employee;
import com.example.demo.repo.EmployeeRepo;

@Service
public class EmployeeMgmtService {

    @Autowired
    private EmployeeRepo repo;

    public String saveEmp(Employee emp) {
        return "Emp with id "+repo.save(emp).getId()+" saved";
    }
}

TestRunner.java

package com.example.demo.runner;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.example.demo.entity.Employee;
import com.example.demo.service.EmployeeMgmtService;

@Component
public class TestRunner implements CommandLineRunner{

    @Autowired
    private EmployeeMgmtService service;

    @Override
    public void run(String... args) throws Exception {
        String[] proj = {"p1","p2","p3"};
        List<String> companies = Arrays.asList("c1","c2","c3");
        Set<Long> contactDetails = Set.of(112233L,334455L);
        Map<String,String> cardDetails = Map.of("aadhar","11223344","pan","A1B2");

        Employee emp = new Employee(new Random().nextInt(1000),"SAM",proj,companies,contactDetails,cardDetails);

        System.out.println(service.saveEmp(emp));
    }

}

Did you find this article valuable?

Support realNameHidden by becoming a sponsor. Any amount is appreciated!