MongoDB query with regex expression in ObjectId

Is it possible to execute a query like:

db.artigo.find( { _id : ObjectId('520a504a3004bc615fcfcf16') } )

but using regex ObjectId?

For example, you will get _ids that contains "004" in this position above.

PS. The reason is the introduction of a short service based on some fields, namely _id. I am trying to create an implicit short service instead of an explicit one (with a field generated for this purpose).

+4
source share
3 answers
  • ObjectId is not a string, but a special type in MongoDB. You can not
    query the regular expression expression operator in a field containing ObjectId's.
  • ... _id ObjectId, _id,
    .
+3

, , ; @mongodb , (https://jira.mongodb.org/browse/SERVER-1146). , -, , , objectIds , , .

, , :

, _id . , 6 _id; , , , , .

+1

This is what you need:

db.artigo.find( { _id : { $regex: '004' } } )

-1
source

All Articles