2

I want to search if there is an object with _id containing a string. For example:

I want to use the string '00d0' to find the object with _id 5af61cc9e4e6d552d70900d0, wich ends with '00d0'

However, 5af61cc9e4e6d552d70900d0 is an ObjectId, and mongoose gives me a 501 when I do this:

Object.find({ _id: { $regex: '00d0', $options: 'i' } })

How should I modify this search query to get it work? Thanks

thousight
  • 1,134
  • 1
  • 14
  • 39
  • What possible use would you think that would be? Those last for characters are partly "random" and partly incremented in sequence. An ObjectId is not a "string" anyway. See [`ObjectId`](https://docs.mongodb.com/manual/reference/method/ObjectId/) in the documentation. – Neil Lunn May 12 '18 at 00:12
  • Whilst you can do things like `.find({ "$where": "/00d0/.test(this._id.str)" })`, that really has no practical usage. 9/10 times what you "really mean" is the ["timestamp range"](https://stackoverflow.com/questions/8749971/can-i-query-mongodb-objectid-by-date) from the very beginning of an `ObjectId` value, as this holds the only useful information from a "query" perspective. – Neil Lunn May 12 '18 at 00:18
  • Also, pretty certain I've already seen this question this week. Deleting and reposting does not get you a different answer as what is shown is the only answer there is. – Neil Lunn May 12 '18 at 00:19

0 Answers0