Query MongoDB with a regex expression against an ObjectId

前端 未结 3 1739
暖寄归人
暖寄归人 2020-12-18 01:04

Is it possible to do a query like:

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

but using a regex on ObjectId?

<
3条回答
  •  执笔经年
    2020-12-18 01:47

    • ObjectId is not a string but a special type in MongoDB. You can not
      query with a regex expression operator against a field that contains ObjectId's.
    • But... _id doesn't have to be an ObjectId, so what i would suggest is providing your own unique string as _id then you can use a regex
      expression for querying.

提交回复
热议问题