MongoDB - 在内存中的BsonDocument中查询

时间:2011-05-10 15:24:05

标签: mongodb mongodb-.net-driver

我正在将一个文档读入BsonDocument对象。从MongoDB读取文档后,我想在内存中查询文档。 我的文档看起来像这样:

{
  "_id": {
    "$binary": "DYibd4bSz0SFXTTmY46gOQ==",
    "$type": "03"
  },
  "title": "XYZ 2011",
  "pages": [
    {
      "pagetype": "contactcapture",
      "pagetitle": "Contact",
      "questions": [
        {
          "qtype": "text",
          "text": "Firstname",
          "name": "firstname"
        },
        {
          "qtype": "text",
          "text": "Surname",
          "name": "surname"
        },
        {
          "qtype": "text",
          "text": "Company",
          "name": "companyname"
        }
      ]
    },
    {
      "pagetype": "question",
      "pagetitle": "Question 1",
      "questions": [
        {
          "qtype": "radio",
          "text": "What drink?",
          "name": "drink",
          "answers": [
            {
              "text": "Tea"
            },
            {
              "text": "Coffee"
            },
            {
              "text": "Hot chocolate"
            },
            {
              "text": "Water"
            }
          ]
        }
      ]
    },
    {
      "pagetype": "question",
      "pagetitle": "Question 2",
      "questions": [
        {
          "qtype": "check",
          "text": "Accompaniments?",
          "name": "accompaniments",
          "answers": [
            {
              "text": "Nuts"
            },
            {
              "text": "Crisps"
            },
            {
              "text": "Biscuits"
            }
          ]
        }
      ]
    },
    {
      "pagetype": "question",
      "pagetitle": "Question 3",
      "questions": [
        {
          "qtype": "radio",
          "text": "When would you like that?",
          "name": "when",
          "answers": [
            {
              "text": "Immediately"
            },
            {
              "text": "10 minutes"
            },
            {
              "text": "Half-an-hour"
            }
          ]
        },
        {
          "qtype": "text",
          "text": "Anything else with that?",
          "name": "anythingelse"
        }
      ]
    }
  ]
}

我希望获得所有包含pagetype =“question”的网页。我目前正在这样做:

BsonDocument profileDocument= profilesCollection.FindOneByIdAs<MongoDB.Bson.BsonDocument>(binaryId);
BsonElement pagesElement = profileDocument.GetElement("pages");
BsonArray pages=profileDocument.GetElement("pages").Value.AsBsonArray;
foreach (BsonValue pageV in pages.Values)
{
    BsonDocument page = pageV.AsBsonDocument;
    if (page["pagetype"].AsString == "question")
    {
        sb.Append("<br />Question Page:" + page.ToJson());
    }
}

代码似乎有点冗长和复杂 - 我只是想知道是否有更好的方法来做到这一点?谢谢

1 个答案:

答案 0 :(得分:0)

假设profilesCollection的数据类型是MongoCollection&lt; BsonDocument&gt;,您可以缩短代码,如下所示:

        var profileDocument = profilesCollection.FindOneById(binaryId);
        foreach (BsonDocument page in profileDocument["pages"].AsBsonArray) {
            if (page["pagetype"].AsString == "question") {
                sb.Append("<br />Question Page:" + page.ToJson());
            }
        }