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
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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")
回答2:
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();
回答3:
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.