mongodb:通过子字符串查询objectid
我希望能够通过我的MongoDB集合中的子字段查询_id字段。但是,由于objectid是它自己的对象,而不是纯字符串,因此很难。这是我目前无济于事的查询,但希望它可以更加清楚我要完成的工作。
{_id: {$regex: '62a0ed7'}}
非常感谢!
I want to be able to query the _id field via a substring in my MongoDB collection. However since the ObjectId is it's own object and not a pure string this is difficult. This is the Query I'm currently using to no avail, but hopefully it gives more clarity on what I'm trying to accomplish.
{_id: {$regex: '62a0ed7'}}
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能
用 REGEX表达式查询运算符对包含Objectid的字段。
我建议提供自己的唯一字符串作为 _id ,然后您可以使用 Regex
查询的表达。
示例:
一个更快的解决方案是您还可以添加一个pre create钩子或其他任何东西来保存ID(_id除外),该iD包含objectid(_id)作为字符串,并且可以在其上进行文本索引并执行搜索而不是正则。
You can not
query with a regex expression operator against a field that contains ObjectId's.
I suggest providing your own unique string as _id then you can use a regex
the expression for querying.
Example:
A faster solution is that you can also add a pre create hook or whatever to save an id (other than the _id) containing the value of the ObjectId (_id) as a string and you can make a text index on it and do search instead of regex.