How can you load initial data in MongoDB through Spring Boot?

you can define your data in json/xml and use populator elements of the repository to load the data.

You can use a repository populator with Spring Data MongoDB. Let me demonstrate this with a code sample in Kotlin:

class TestApplicationConfig {
    private lateinit var testData: Resource

    fun repositoryPopulator(objectMapper: ObjectMapper): Jackson2RepositoryPopulatorFactoryBean {
        val factory = Jackson2RepositoryPopulatorFactoryBean()
        // inject your Jackson Object Mapper if you need to customize it:
        return factory

Put test_data.json in resources directory.

To load initial data you can use db migration tool like MongoBee

It's very useful option to handle data initialization in java. You just need to configure @Bean public Mongobee mongobee in your spring boot and setup component scan for data ChangeLogs where data creation actually happens.