Handle ObjectId in spring mongo repository @Query

14 Views Asked by At

The idea is pretty simple - to have null safe and flexible api.

Mongo automatically generates ids fro records in collection (example - 65df764b69c8ed15c6d61f4d)

to fetch it from db i'm using query - { _id : ObjectId("65df764b69c8ed15c6d61f4d") } in 3t studio.

I'm trying to apply the same in spring mongo repository

@Query("{ $or : [ { $where: '?0 == null' } , { '_id' : ObjectId(?0) } ] }")

But i'm failing with:

Caused by: com.mongodb.util.JSONParseException: 
{ $or : [ { $where: '"_param_0" == null' } , { '_id' : ObjectId("_param_0") } ] }
                                                       ^

Please help.

0

There are 0 best solutions below