1

I need to be able to do search on ids based on a regex (I want to preform a "like" operation on the id).
I tried using regex

db.getCollection('aaa').find( {_id:/5a741dcd395b01425ee66770/})

I also try with regex

db.getCollection('aaa').find( {_id:{$regex:"5a741dcd395b01425ee66770"}})

I know the id is a mongo object and that it why it wont work. But I still need this functionality.

I thought maybe create another id that save the id as a string so that I have 2 ids fields, one for search and 1 when the insert occurs, but this complicate staff as when my code do insert I will then need to do update on it from the result id. Is there a way to do it automatically or a better way to achieve this ?

TGrif
  • 5,725
  • 9
  • 31
  • 52
Amit Wagner
  • 3,134
  • 3
  • 19
  • 35
  • 1
    you can't do regex search on ObjectId. If you really need this regex search functionnality, you should store the hex string along with the _id in the document. – felix Feb 02 '18 at 10:05
  • You can refer https://stackoverflow.com/questions/3305561/how-to-query-mongodb-with-like – Kiran LM Feb 02 '18 at 10:33

0 Answers0