如何对mongodb ObjectId字段进行正则表达式查询 [英] how to do a regex query on mongodb ObjectId field

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

问题描述

这有点棘手..如何在ObjectId字段上进行正则表达式查询?

this is a bit tricky.. how do you do a regex query on the ObjectId field?

我正在使用Java api,所以到目前为止,这就是我

i'm using the java api, so this is what i have so far

BasicDBObject q = new BasicDBObject()
q.put(field, Pattern.compile(value, Pattern.CASE_INSENSITIVE));

这对于任何常规字段都适用.但似乎不适用于ObjectId字段.我认为是因为我无法将ObjectId与字符串进行比较?而且我不能完全将部分ID或正则表达式放入新的ObjectId中.它只会抛出一个错误.

this works fine for any regular field. but doesn't seem to work with ObjectId field. which i assume is because i can't compare an ObjectId to a string? and i can't exactly just put a partial id or a regex into a new ObjectId. it'll just throw an error.

对此有什么想法吗?我正在尝试为用户提供一种输入ID的方式,并能够以该模式取回所有文档.

any ideas on this? i'm trying to give users a way to enter part of an id, and be able to get back all documents with that pattern.

提前谢谢!

推荐答案

据我所知,ObjectId是MongoDB中的一个自己的类型,并不像字符串那样.而且,尽管MongoDB中还有其他类型与字符串不同,例如数组等,它们可以通过正则表达式模式进行搜索,但对于ObjectId来说似乎是不可能的.为了实现像您一样的ID搜索,我可能会定义一个自己的索引ID字段,其中包含一个ID(如简单字符串)和冷肩ObjectId.

As far as I know ObjectId is a own type in MongoDB and does not behave like a string. And although there are other types in MongoDB distinct from string, like arrays and so on, which are searchable by a regex pattern, this seems not possible for ObjectId. To implement an id search like yours I would probably define an own, indexed id field which contains an id as a simple string and cold-shoulder ObjectId.

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

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