I have studied on mongodb with spring boot
I install mongo with docker-compose, I use Mongo Compass to interactive with mongo database, and I create product database
version: "3.8"
services:
mongodb:
image : mongo
container_name: mongodb
environment:
- PUID=1000
- PGID=1000
volumes:
- ./database/product-service/mongodb:/data/db
ports:
- 27017:27017
restart: unless-stopped
Spring Boot code
Controller
@RestController
@RequestMapping("/api/v1/product")
@RequiredArgsConstructor
public class ProductController {
private final ProductService productService;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void createProduct(@RequestBody ProductRequest productRequest) {
productService.createProduct(productRequest);
}
@GetMapping
@ResponseStatus(HttpStatus.OK)
public List<ProductResponse> getAllProducts() {
return productService.getAllProducts();
}
}
Service
@Service
@RequiredArgsConstructor
@Slf4j
public class ProductService {
private final ProductRepository productRepository;
public void createProduct(ProductRequest productRequest) {
Product product = Product.builder()
.name(productRequest.getName())
.description(productRequest.getDescription())
.price(productRequest.getPrice())
.build();
productRepository.save(product);
log.info("Product {} is saved", product.getId());
}
public List<ProductResponse> getAllProducts() {
List<Product>products = productRepository.findAll();
log.info("Get all products");
return products.stream().map(product -> map2ProductResponse(product)).toList();
}
private ProductResponse map2ProductResponse(Product product) {
return ProductResponse.builder()
.id(product.getId())
.name(product.getName())
.description(product.getDescription())
.price(product.getPrice())
.build();
}
}
entity
@Document(value = "product")
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class Product {
private String id;
private String name;
private String description;
private BigDecimal price;
}
With spring boot app, I configed on appllication.properties file like that
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/product
Because I have found some posts
I have tried replace with this, nothing change
spring.data.mongodb.port=27017
spring.data.mongodb.database=product
spring.data.mongodb.host=localhost
when I check the Mongo Compass, it always connect to test database, although I specify product database like application.properties
I have also found the similar question, I have tried with that solution, it not working for me and the solution is too complicated for me. It raise a error
Error creating bean with name 'mongoConfig': Injection of autowired dependencies failed
And I can not fix that problem, because I’m new with mongo
How I can change spring boot app connect to another database?
2
Answers
I have tried to read mongo for a couple hours docs, I have found the solution.
Almost of authentication mechanisms, we can not set database name
For example with default authentication mechanism using connection string or mongo credential, I don't see the parameter for database name, only include username, authenticationDb, and password
With connection string:
With MongoCredential:
Other mechanisms are same.
Thus, I have tried to create config file
and add database name in application.properties
Its amazing, it work perfectly like I want