SoFunction
Updated on 2025-05-09

MongoDB Integration of SpringBoot for Examples

Reaching dependencies

        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

Yaml configuration

spring:
    data:
      mongodb:
        uri: mongodb://localhost:27017/chat_memory_db # mongodbaddress Will be created automatically

database

Define the document (the document is equivalent to the line of MYSQL)

Define the document entity

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document("chat_message")
public class ChatMessages {
    @Id
    private Long messageId;
    /**
      * Chat record list json string
      */
    private String content;
}
    @Test
    public void test(){
        ChatMessages chatMessages = new ChatMessages(1L,"Hello");
        (chatMessages);
    }

Add, delete, modify and check

public void testInsert(){
        ChatMessages chatMessages = new ChatMessages();
        ("Hello");
        (chatMessages);
    }
    @Test
    public void testQuery(){
        ChatMessages byId = ("680e38d1eb0e2f7b6d287541", );
        (byId);
    }
    @Test
    public void testUpdate1(){
        // Modify according to id        Criteria criteria = ("_id").is("680e38d1eb0e2f7b6d287541");
        Query query = new Query(criteria);
        Update update = new Update();
        ("content","New Chat Record");
        // Add or update        (query,update,);
    }
    @Test
    public void testUpdate2(){
        // Modify according to id        Criteria criteria = ("_id").is("100");
        Query query = new Query(criteria);
        Update update = new Update();
        ("content","New Chat Record");
        // Add or update        (query,update,);
    }
    @Test
    public void testRemove(){
        // Modify according to id        Criteria criteria = ("_id").is("100");
        Query query = new Query(criteria);
        Update update = new Update();
        ("content","New Chat Record");
        // Add or update        (query,);
    }

This is the article about MongoDB integrating SpringBoot. This is all about this article. For more related MongoDB integrating SpringBoot content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!