【问题标题】:MongoDB - Query within an in-memory BsonDocumentMongoDB - 在内存 BsonDocument 中查询
【发布时间】:2011-05-10 15:24:05
【问题描述】:

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

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

【问题讨论】:

    标签: mongodb mongodb-.net-driver


    【解决方案1】:

    假设profilesCollection的数据类型是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());
                }
            }
    

    【讨论】:

    • 感谢您的改进。我真的希望有某种方法可以使用诸如 profileDocument["pages"].Filter("{pagetype:"question"}) 之类的语法对内存中对象执行查询,但看起来没有t. 无论如何,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    相关资源
    最近更新 更多