2

We can generate ObjectId from client and use it while insert. I done want it to be handled outside of insertion process. I need it to be configured as my default _id generation process so that when ever I call insert, insert method should create the custom objectId and use it. Is it possible with mongodb's Java driver?

I need to somehow override the default _id generation process. I don't like to explicitly define _id in the inserting document.

Rasekaran
  • 334
  • 2
  • 12
  • Possible duplicate of [MongoDB: insert documents with specific id instead of auto generated ObjectID](https://stackoverflow.com/questions/26273821/mongodb-insert-documents-with-specific-id-instead-of-auto-generated-objectid) – dnickless Jan 08 '18 at 19:56
  • 2
    @dnickless I have gone thorough that question before posting this here. That explains add custom _id by explicitly defining in the document. I don't like to explicitly define in the document. I want to override default _id generation process so that even if there is no _id defined in inserting document, it will create a new _id using the override _id generator. – Rasekaran Jan 09 '18 at 05:31

1 Answers1

1

That is currently not possible directly through the driver.

Despite the fact that there is an IdGenerator interface in place which could be used to create a custom implementation there is currently no way to inject that custom instance into the driver.

You can see that in the MongoDB Java Driver repository.

There was a JIRA ticket to support this which, however, got closed as a duplicate of another ticket which again is closed as "Won't fix".

You should be able, though, to implement your repository in a way that it assigns a custom id before saving anything.

dnickless
  • 10,733
  • 1
  • 19
  • 34