MongoDB - 在内存 BsonDocument 中查询

发布于 2024-11-06 01:56:37 字数 2743 浏览 0 评论 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());
    }
}

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

I am reading a single document into a BsonDocument object. Having read the document from MongoDB I'd like to query the document in-memory.
My doc looks like this:

{
  "_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"
        }
      ]
    }
  ]
}

I want to get all pages that have a pagetype="question". I'm currently doing it as follows:

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());
    }
}

The code seems a little verbose and complex - I just wondered if there is a better way of doing this? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫尐诺 2024-11-13 01:56:37

假设 profileCollection 的数据类型是 MongoCollection,您可以缩短代码,如下所示:

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

Assuming that the data type of profilesCollection is MongoCollection<BsonDocument>, you could shorten the code so something like this:

        var profileDocument = profilesCollection.FindOneById(binaryId);
        foreach (BsonDocument page in profileDocument["pages"].AsBsonArray) {
            if (page["pagetype"].AsString == "question") {
                sb.Append("<br />Question Page:" + page.ToJson());
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文