MongoDB C# 查询“Like”串上

发布于 2024-12-19 10:42:34 字数 94 浏览 2 评论 0原文

我正在使用官方 mongodb C# 驱动程序。 我想查询类似于 SQL 的 mongodb C# 驱动程序中类似于 db.users.find({name:/Joe/}

i am using official mongodb c# driver.
i want to query mongodb simliar to SQL Like
something like db.users.find({name:/Joe/} in c# driver

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

允世 2024-12-26 10:42:34

c# 查询将如下所示:

Query.Matches("name", BsonRegularExpression.Create(new Regex("Joe")));

更新:

根据 @RoberStam 建议,有更简单的方法可以执行此操作:

Query.Matches("name", "Joe") 

c# query will looks like:

Query.Matches("name", BsonRegularExpression.Create(new Regex("Joe")));

Update:

As per @RoberStam suggestion, there is more simple way to do this:

Query.Matches("name", "Joe") 
假装不在乎 2024-12-26 10:42:34

对于 c# 驱动程序 2.1 (MongoDB 3.0)

var collection = database.GetCollection<BsonDocument>("<<name of the collection>>");

var filter = Builders<BsonDocument>.Filter.Regex("name", new BsonRegularExpression("Joe"));

var result = await collection.Find(filter).ToListAsync();

对于 c# 驱动程序 2.2 (MongoDB 3.0)

var filter = new BsonDocument { { parameterName, new BsonDocument { { "$regex", value }, { "$options", "i"} } } }

var result = collection.Find(filter).ToList();

For the c# driver 2.1 (MongoDB 3.0)

var collection = database.GetCollection<BsonDocument>("<<name of the collection>>");

var filter = Builders<BsonDocument>.Filter.Regex("name", new BsonRegularExpression("Joe"));

var result = await collection.Find(filter).ToListAsync();

For the c# driver 2.2 (MongoDB 3.0)

var filter = new BsonDocument { { parameterName, new BsonDocument { { "$regex", value }, { "$options", "i"} } } }

var result = collection.Find(filter).ToList();
一身软味 2024-12-26 10:42:34

MongoDB C# 驱动程序有一个可以使用的 BsonRegex 类型

正则表达式是最接近 SQL LIKE 语句的。

请注意,带前缀的正则表达式可以使用索引:/^Joe/ 将使用索引,/Joe/ 不会。

MongoDB C# driver has a BsonRegex type that you can use.

Regex is the closest you will get to the SQL LIKE statement.

Note that prefixed Regexes can use indexes: /^Joe/ will use an index, /Joe/ will not.

分開簡單 2024-12-26 10:42:34

假设我需要从 Mongodb 文档的属性中搜索变量“textToSearch”的值。

示例:我们必须在JobModel.Title包含manager的所有记录中搜索manager。这是记录中的 textToSearch=manager。 ( textToSearch 是一个字符串类型。我在答案末尾添加了一些正则表达式。为了涵盖 textToSearch 包含、textToSearch 开头以及更多场景)

等效 C# 代码:

Note: I have also shown how you can append to your existing filter, ignore it if not required.

var mongoBuilder = Builders<BsonDocument>.Filter;
var filter = mongoBuilder.Eq(y => y.JobModel.Category, "full time");

if (!string.IsNullOrEmpty(textToSearch))
{
    textToSearch = "/.*" + textToSearch + ".*/i"; // this regex will search all the records which contains textToSearch and ignores case
    filter = filter & mongoBuilder.Regex(y => y.JobModel.Title, new BsonRegularExpression(textToSearch));
}                

等效的 Mongo 查询代码:

db.jobs.find({ "JobModel.Category" : "full time", 
"JobModel.Title" : /.*manager.*/i })  

一些有用的正则表达式:

  • 此正则表达式将搜索包含 textToSearch 的所有记录并忽略大小写。 textToSearch = "/.*" + textToSearch + ".*/i";
  • 此正则表达式将搜索以 textToSearch 开头的所有记录并忽略大小写。 textToSearch = "/^" + textToSearch + "/i";
  • 此正则表达式将搜索以 textToSearch 开头的所有记录,并且不忽略大小写。 textToSearch = "/.*" + textToSearch + ".*/";

Suppose I need to search the value of variable 'textToSearch' from a property of Mongodb documents.

Example: We have to search manager in all the records where JobModel.Title contains manager. That is textToSearch=manager from the records. ( textToSearch is a string type. I have added some Regexs at the end of my answer. To cover textToSearch contains, textToSearch starts with and few more scenarios)

Equivalent C# Code:

Note: I have also shown how you can append to your existing filter, ignore it if not required.

var mongoBuilder = Builders<BsonDocument>.Filter;
var filter = mongoBuilder.Eq(y => y.JobModel.Category, "full time");

if (!string.IsNullOrEmpty(textToSearch))
{
    textToSearch = "/.*" + textToSearch + ".*/i"; // this regex will search all the records which contains textToSearch and ignores case
    filter = filter & mongoBuilder.Regex(y => y.JobModel.Title, new BsonRegularExpression(textToSearch));
}                

Equivalent Mongo Query Code:

db.jobs.find({ "JobModel.Category" : "full time", 
"JobModel.Title" : /.*manager.*/i })  

Some Useful Regex:

  • this regex will search all the records which contains textToSearch and ignores case. textToSearch = "/.*" + textToSearch + ".*/i";
  • this regex will search all the records which starts with textToSearch and ignores case. textToSearch = "/^" + textToSearch + "/i";
  • this regex will search all the records which starts with textToSearch and do not ignores case. textToSearch = "/.*" + textToSearch + ".*/";
暮色兮凉城 2024-12-26 10:42:34

感谢@Sridhar - 类似的方法对我有用

public List<SearchModel> GetSearchResult(string searchParam) => _collection.Find(new BsonDocument { { "Datetime", new BsonDocument { { "$regex", searchParam }, { "$options", "i" } } } }).ToList(); // Search DateTime "Like"

Thanks to @Sridhar - similar aproach that worked for me

public List<SearchModel> GetSearchResult(string searchParam) => _collection.Find(new BsonDocument { { "Datetime", new BsonDocument { { "$regex", searchParam }, { "$options", "i" } } } }).ToList(); // Search DateTime "Like"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文