如何在MongoDB中查询类似的问题c#

时间:2015-11-06 11:21:00

标签: c# mongodb mongodb-.net-driver

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

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

它很棒!!我如何在c#中完成?

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

不退货!

2 个答案:

答案 0 :(得分:2)

Filter Definition Builder 与正则表达式过滤器 Regex() 一起使用,如下所示:

var search = "M";
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Regex("city", search);

答案 1 :(得分:1)

//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 "."