How to query MongoDB with “like”?

后端 未结 30 2180
予麋鹿
予麋鹿 2020-11-21 05:51

I want to query something with SQL\'s like query:

SELECT * FROM users  WHERE name LIKE \'%m%\'

How to do I achieve the same in

30条回答
  •  自闭症患者
    2020-11-21 06:15

    Use regular expressions matching as below. The 'i' shows case insensitivity.

    var collections = mongoDatabase.GetCollection("Abcd");
    
    var queryA = Query.And(
             Query.Matches("strName", new BsonRegularExpression("ABCD", "i")), 
             Query.Matches("strVal", new BsonRegularExpression("4121", "i")));
    
    var queryB = Query.Or(
           Query.Matches("strName", new BsonRegularExpression("ABCD","i")),
           Query.Matches("strVal", new BsonRegularExpression("33156", "i")));
    
    var getA = collections.Find(queryA);
    var getB = collections.Find(queryB);
    

提交回复
热议问题