Handling Both String and ObjectId for _id in NestJS with MongoDB

I’m working on a NestJS application with MongoDB, and I have a situation where my old database stores _id as a string, while MongoDB by default assigns an ObjectId to _id for new documents.

I want my application to handle both cases without converting the string _id into ObjectId because I need to maintain compatibility with existing data.

I’ve tried some approaches like:

Using mixed types for _id in my schema (_id: string | ObjectId)
Querying with $in: [ObjectId(id), id]
Using custom serialization in my NestJS DTO
But these haven’t worked as expected.

What’s the best way to structure my NestJS + MongoDB application so it can handle both string and ObjectId as _id without issues?

Any suggestions or best practices would be really helpful!