POM file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Copy the code

The Demo entity

@Data
/ / the document name
@Document("demo_document")
@Accessors(chain = true)
public class DemoDocument implements Serializable{
    
    @Id
    private String id;
    /* Sample fields */
    // Business type
    private String bizType;
    / / configuration items
    private JSONObject config;
}
Copy the code

The persistence layer

@Repository
public interface DemoDocumentRepository extends MongoRepository<DemoDocument.String>{

    // Customize the query
    @Query({"{'bizType':? 0}})"
    List<DemoDocument> findAllByBizType(String bizType);
}
Copy the code

The Service layer using

Here lazy, do not do the interface

@Service
public class DemoDocumentService {
    @Autowired
    private DemoDocumentRepository demoDocumentRepository;
    
    / / new
    public DemoDocument insertDemo(DemoDocument demo){
        return demoDocumentRepository.insert(demo);
    }
    
    // Delete by id
    public void delete(String id){
        demoDocumentRepository.deleteById(id);
    }
    
    // Get a collection of documents based on the business type
    public List<DemoDocument> listByBizType(String bizType){
        returndemoDocumentRepository.findAllByBizType(bizType); }}Copy the code