MongoTemplateでこれを行うことができます。
ここでは春のリポジトリの例です:
@Autowired
private MongoTemplate mongoTemplate;
@PostConstruct
public void addMarketsToDB() {
if (mongoTemplate.findOne(new Query(), MarketEntity.class) == null) {
URL url = this.getClass().getResource("/marches.json");
byte[] encoded;
try {
encoded = Files.readAllBytes(Paths.get(url.toURI()));
BasicDBObject obj = new BasicDBObject();
obj.append("$eval", new String(encoded));
mongoTemplate.executeCommand(obj);
} catch (IOException | URISyntaxException e) {
LOGGER.error("{} addMarketsToDB -> failed to read file", LOG_HEADER);
}
}
}