mongo-jackson-codec icon indicating copy to clipboard operation
mongo-jackson-codec copied to clipboard

Get Id of inserted document

Open leangl opened this issue 9 years ago • 1 comments

Hi, I'm using the @Id annotation leaving the generation to Mongo, but I'm not sure how to get the generated Id after inserting. Before switching to the jackson codec, I was using just a plain Document and I was able to get the _id field after doing the insertion.

Any idea? Thanks.

leangl avatar Jun 10 '16 15:06 leangl

Hi

@Id doesn't leave the generation to Mongo. It only "map" the field.

The idea is that often, you want to be able to generate ids on the fly, so people use something like private String id = UUID.randromUUID().toString()'. But mongo basically expectObjectId` type.

So, the @Id allow to convert from/to String<->ObjectId, but that's all.

ylemoigne avatar Jun 28 '16 15:06 ylemoigne