如何在MongoDB c#中进行类似的查询

本文关键字:查询 MongoDB | 更新日期: 2023-09-27 18:21:00

在MongoDB中,如果我想做一个LIKE语句,我会做:

 db.users.find({"name": /m/})

而且效果很好!!我如何在c#中做到这一点?

Builders<BsonDocument>.Filter.Eq("city", "/M/"); 

不退还任何东西!

如何在MongoDB c#中进行类似的查询

使用过滤器定义生成器和正则表达式过滤器Regex(),如下所示:

var search = "M";
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Regex("city", search);
//Use Regex,
var search = "M";
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Regex("city", "^" + search + ".*"); 
// ^ - For Starts with
// . - any character
// * - for any number of previous "."