# Spring Boot MongoDB ``` dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' } ``` ## install mongodb ``` xcode-select --install brew tap mongodb/brew brew install mongodb-community@4.4 ``` ## Code ```java import java.util.List; import org.springframework.data.mongodb.repository.MongoRepository; public interface CustomerRepository extends MongoRepository { public Customer findByFirstName(String firstName); public List findByLastName(String lastName); } ``` ```java @SpringBootApplication public class AccessingDataMongodbApplication implements CommandLineRunner { @Autowired private CustomerRepository repository; public static void main(String[] args) { SpringApplication.run(AccessingDataMongodbApplication.class, args); } @Override public void run(String... args) throws Exception { repository.deleteAll(); // save a couple of customers repository.save(new Customer("Alice", "Smith")); repository.save(new Customer("Bob", "Smith")); // fetch all customers System.out.println("Customers found with findAll():"); System.out.println("-------------------------------"); for (Customer customer : repository.findAll()) { System.out.println(customer); } System.out.println(); // fetch an individual customer System.out.println("Customer found with findByFirstName('Alice'):"); System.out.println("--------------------------------"); System.out.println(repository.findByFirstName("Alice")); System.out.println("Customers found with findByLastName('Smith'):"); System.out.println("--------------------------------"); for (Customer customer : repository.findByLastName("Smith")) { System.out.println(customer); } } } ``` ## ref - https://spring.io/guides/gs/accessing-data-mongodb/ - https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/