Query MongoDB with a regex expression against an ObjectId

前端 未结 3 1740
暖寄归人
暖寄归人 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:59

    I believe this is one of the areas where mongodb is lacking; and also the team @ mongodb looks like they won't support this feature as this (https://jira.mongodb.org/browse/SERVER-1146) jira issue indicates. The argument seems to be to use our own id that is of string format but objectIds make sense in big systems where you have large volumes of transactions e.g. Amazons order numbers

    The Solution I cam up with is as follows:

    I created another column that copies the first six digits of my _id field and is of string type . The reason I choose the first 6 digits of my _id field are; one to save space and be compact and also at the end of the day the reason I need a regex expression is for searching purpose and that fits well for my needs.

提交回复
热议问题