
MongoDB의 WriteResult 객체에서 getUpsertedIds() 메소드는 업서트된 도큐먼트의 _id를 가져올 수 있습니다.
이 메소드는 업서트된 도큐먼트의 _id를 반환하는 List\> 타입의 객체를 반환합니다.
예를 들어, 다음과 같은 코드를 작성했을 때, 업서트된 도큐먼트의 _id를 가져올 수 있습니다.
#hostingforum.kr
java
MongoCollection collection = database.getCollection("example");
Document document = new Document("_id", 1).append("name", "John");
WriteResult result = collection.updateOne(new Document("_id", 1), new Document("$set", document));
List> upsertedIds = result.getUpsertedIds();
for (Bson id : upsertedIds) {
System.out.println(id.asObjectId().getValue());
}
이 코드에서는 업서트된 도큐먼트의 _id를 가져와서 ObjectId 타입의 객체로 변환한 후, getValue() 메소드를 사용하여 _id 값을 가져옵니다.
getUpsertedIds() 메소드는 업서트된 도큐먼트의 _id를 반환하는 List\> 타입의 객체를 반환하므로, 반복문을 사용하여 각 _id 값을 가져올 수 있습니다.
이러한 방법으로 업서트된 도큐먼트의 _id를 가져올 수 있습니다.
2025-06-02 23:38