mongo-jackson-codec
mongo-jackson-codec copied to clipboard
Get Id of inserted document
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.
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.