MongoDB c#查询子文档

时间:2015-12-03 22:58:43

标签: mongodb

我有一个mongodb文档,例如

"_id" : ObjectId("565e6c9b10bcd93ca8d1206c"),
"listHeader" : [ 
{
        "strName" : "MSID",
        "strVal" : "154800"
}, 
{
        "strName" : "Operation",
        "strVal" : "8221"
}, 

}

我想查询MSID = 154800和Operation = 8221,

如何在C#中做? 我有

var queryMod = Query.And(
Query<ModuleTestData>.EQ(e =>e.MSID , strSerialNumber),
Query<ModuleTestData>.EQ(e => "listHeader.Operation",      
             operation));

不工作?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

使用如下匹配的正则表达式。 &#39; i&#39;显示不区分大小写。

var collections = mongoDatabase.GetCollection(&#34; Abcd&#34;);

var queryA = Query.And(
         Query.Matches("strName", new BsonRegularExpression("MSID", "i")), 
         Query.Matches("strVal", new BsonRegularExpression("154800", "i")));

var queryB = Query.And(
       Query.Matches("strName", new BsonRegularExpression("Operation","i")),
       Query.Matches("strVal", new BsonRegularExpression("8221", "i")));

var getA = collections.Find(queryA);
var getB = collections.Find(queryB);