https://www.mongodb.com/docs/drivers/java/sync/current/quick-start/
Quick Start — Java Sync
If you encounter an error connecting to your MongoDB instance or cluster that resembles the following while running your application, you may need to update your JDK to the latest patch release: javax.net.ssl.SSLHandshakeException: extension (5) should not
www.mongodb.com
처음 모든 언어에서 "Hello wolrd"를 띄우는 것처럼
퀵 스타트를 따라 해 보며 JAVA로 MongoDB 와 연결이 되는지를 확인!
dependencies { implementation 'org.mongodb:mongodb-driver-sync:4.7.1' }
build.gradle 에 내용을 추가.

testDB에 test로 쓸 것을 insert 하고,
public class test { public static void main(String[] args) { String uri = "mongodb://localhost:27017"; try (MongoClient mongoClient = MongoClients.create(uri)) { MongoDatabase database = mongoClient.getDatabase("testDB"); MongoCollection<Document> collection = database.getCollection("testCol"); Document doc = collection.find(eq("name", "test")).first(); System.out.println(doc.toJson()); } } }

!... 됐다.
find에 이어서 다른 CRUD 기능들도 해보자.
Document doc2 = new Document("name", "test2").append("age", "22"); collection.insertOne(doc2); collection.updateOne(Filters.eq("name", "test"), Updates.set("age", "7")); collection.deleteOne(Filters.eq("name", "test2"));



나머지도 정상 작동하는 것을 확인했다!
좀 더 가다듬어서 사용해봐야겠다.
'MongoDB' 카테고리의 다른 글
MongoDB와 Spring Boot를 이용한 프로젝트 만들기 - 1 (0) | 2023.03.17 |
---|---|
스프링부트와 mongodb를 연결해보자! (0) | 2022.11.22 |
MongoDB의 기본을 알아보자 - 1 (0) | 2022.11.02 |