检索包含BSON文档的数组

时间:2016-03-31 14:41:53

标签: c# mongodb

我正在尝试使用C#检索包含BSON文档的数组。下面是文档结构和我用来执行此操作的代码。

"_id": 199,
    "name": "Rae Kohout",
    "scores": [{
        "type": "exam",
        "score": 82.11742562118049
    },
    {
        "type": "quiz",
        "score": 49.61295450928224
    },
    {
        "type": "homework",
        "score": 28.86823689842918
    },
    {
        "type": "homework",
        "score": 5.861613903793295
    }]
}



var db = client.GetDatabase("school");
var studentCol = db.GetCollection<BsonDocument>("students");
var builderDoc = Builders<BsonDocument>.Filter;                       
var filter=builderDoc.ElemMatch("scores", builderDoc.Eq("type","homework" ));
var result = studentCol.Find(filter); 

当我执行此语句时,结果的值将变为{find({ "scores" : { "$elemMatch" : { "type" : "homework" } } })}。如果我应用.tolist(),我会看到数据正常,但作为一个通用集合,我无法对其进行任何mongo查询。

如何将文档检索为数组?

0 个答案:

没有答案