[JAVA-3345] Get ID of last inserted document in a mongoDB w/ Java driver - WITH TYPED COLLECTION Created: 28/Jun/19  Updated: 01/Jul/19  Resolved: 01/Jul/19

Status: Closed
Project: Java Driver
Component/s: Command Operations
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Nagy László Zsolt Assignee: Ross Lawley
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates JAVA-2674 POJO ID property is not populated aft... Closed

 Description   

For typed/generic collections, the @BsonId field is not recognized and changed by the driver. It seems impossible to get the object id of the last inserted document.  For details, please see: https://stackoverflow.com/questions/54362159/get-id-of-last-inserted-document-in-a-mongodb-w-java-driver-with-typed-collec

I'm asking this feature request because I did not get an answer for almost 6 months. I suppose that there is no good solution for this question other than changing the API. This change would probably be backwards compatible (I'm not 100% sure though).



 Comments   
Comment by Ross Lawley [ 01/Jul/19 ]

Closing as a duplicate of JAVA-2674.

This feature was added in 3.10. If you update your driver then you can take advantage of bug fixes and new features.

All the best,

Ross

Comment by Nagy László Zsolt [ 01/Jul/19 ]

We are using 3.6.1 driver, and it does not populate that field for me:

 

<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongo-java-driver</artifactId>
  <version>3.6.1</version>
</dependency>

 

For this reason, we are still inserting documents instead of DTOs and we are reconstructing the DTO later. Here is an example:

 

public ForumMessageDTO addMessage(Long instituteGroupId, Long userId, String message, Document data) { 

    Document d = new Document();
          d.put("instituteGroupId", instituteGroupId);
          d.put("userId", userId);
    d.put("created", new Date());
          d.put("message", message);
    d.put("data", data);
           messages.insertOne(d);
    ForumMessageDTO result = new ForumMessageDTO(instituteGroupId, userId, d.getDate("creted") , message, data);
          result.setId(d.getObjectId("_id"));
   return result;
}

 

The declaration of the DTO starts with this:

 

public class ForumMessageDTO {
  @Expose
  @BsonId 
  private ObjectId id;
  public ObjectId getId() { public ObjectId getId()

{ return id; }

  public void setId(ObjectId id) { this.id = id; }

  ...

 

 

 

 

 

Comment by Ross Lawley [ 01/Jul/19 ]

Hi nagylzs,

This looks to be a duplicate of JAVA-2674. By default in 3.10 Pojos with an ObjectId _id field / @bsonId property will automatically generate and mutate the property on insertion.

Ross

Generated at Thu Feb 08 08:59:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.