Using the "MongoDB Kotlin Driver" is there any best practices to define the data model? Particularly regarding the _id vs id while sharing the same data model to serialize through different serializer.

Ideally the data is received in Json (with id), parsed to data class and save to MongoDB (with _id)

The MongoDB documentation suggest to use:

@SerialName("_id")
@Contextual override val id: String,

But then other serializers (like Json), will also serialize id to _id, which is a Mongo specific artefact that we don't want to share to other system (API, Kafka, ...)

Is there a more elegant pattern than one of those?

  • Code a manual serializer
  • Define 2 dataModel and copy in code between each, just for mapping the _id field name
  • Let mongo auto generate a random _id, and create a extra index on "id"

0

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.