0

我正在将单个文档读入 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());
    }
}

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

4

1 回答 1

0

假设profilesCollection的数据类型是MongoCollection<BsonDocument>,你可以把代码缩短成这样:

        var profileDocument = profilesCollection.FindOneById(binaryId);
        foreach (BsonDocument page in profileDocument["pages"].AsBsonArray) {
            if (page["pagetype"].AsString == "question") {
                sb.Append("<br />Question Page:" + page.ToJson());
            }
        }
于 2011-05-10T22:23:30.740 回答