如何使用MongoRegex(MongoDB C#驱动程序) [英] How to use MongoRegex (MongoDB C# Driver)

查看:70
本文介绍了如何使用MongoRegex(MongoDB C#驱动程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何使用MongoRegex进行文档搜索吗?

Has anyone have any idea how to use MongoRegex for the document search?

我尝试过此操作,但未返回任何内容:

I attempted this, but returns nothing back:

var spec = new Document();
spec.Add("Name", new MongoRegex("/" + searchKey + "*/", "i"));
collection.Find(spec)

想知道为什么它不起作用,我尝试从控制台执行以下命令:

Wondering why it doesn't work, I tried to execute following command from the console:

db.things.find({"Name":/john*/i}) /* WORKS */
db.things.find({"Name":"/john*/i"}) /* DOESN'T WORK */

驱动程序是否可能对正则表达式使用双引号?

Is that possible that the driver applies double quotation to the regex?

谢谢..

推荐答案

您只需要一个简单的前缀查询.然后,您的正则表达式为^ + searchKey.此外,此表单还将允许mongodb在Name上使用索引.

you just want a simple prefix query. Your regex is then ^ + searchKey. Also, this form will allow mongodb to use an index on Name.

var spec = new Document("Name", new MongoRegex(string.Format("^{0}",searchKey), "i"));
collection.Find(spec)

这篇关于如何使用MongoRegex(MongoDB C#驱动程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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