MongoDB - 在内存 BsonDocument 中查询
我正在将单个文档读入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设 profileCollection 的数据类型是 MongoCollection,您可以缩短代码,如下所示:
Assuming that the data type of profilesCollection is MongoCollection<BsonDocument>, you could shorten the code so something like this: