MongoDB C# Query for 'Like' on string

后端 未结 3 695
忘掉有多难
忘掉有多难 2020-12-09 09:06

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

3条回答
  •  猫巷女王i
    2020-12-09 09:35

    For the c# driver 2.1 (MongoDB 3.0)

    var collection = database.GetCollection("<>");
    
    var filter = Builders.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();
    

提交回复
热议问题