使用ObjectId的正则表达式查询MongoDB [英] Query MongoDB with a regex expression against an ObjectId

查看:106
本文介绍了使用ObjectId的正则表达式查询MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以执行以下查询:

Is it possible to do a query like:

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

但是在ObjectId上使用正则表达式吗?

but using a regex on ObjectId?

例如,获取上面那个位置上包含"004"的_id.

For example, do get _ids that contains "004" on that position above.

PS.原因是要基于某些字段(即_id)实现矮人服务.我正在尝试创建一个隐式的"shorty"服务,而不是一个显式的服务(为此生成了一个字段).

PS. The reason is to implement a shorty service based on some fields, namely the _id. I'm trying to create an implicit "shorty" service instead of an explicit one (with a field generated for the purpose).

推荐答案

我认为这是mongodb缺乏的领域之一;并且@mongodb团队似乎不支持此功能,因为此( https://jira.mongodb.org/browse/SERVER-1146 )jira问题指示. 该参数似乎是使用我们自己的字符串格式的id,但是objectIds在大型系统中有意义,在大型系统中您需要进行大量事务处理,例如亚马逊订单号

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

我提出的解决方案如下:

我创建了另一列,该列复制_id字段的前六位数字,并且为字符串类型.我选择_id字段的前6位数字的原因是;一个节省空间和紧凑的功能,而且到了最后,我需要一个正则表达式的原因是出于搜索目的,非常适合我的需求.

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.

这篇关于使用ObjectId的正则表达式查询MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆