我花费了许多时间寻找答案......在PHP中这很容易,但是我无法在C#(我是C#和MongoDB的新手)中做到这一点。我正在尝试遍历存储文档的所有级别。该文档如下:
{
"_id": ObjectId("51f90101853bd88971ecdf27"),
"fields": [
{
"ID": ObjectId("51fd09498b080ee40c00514e"),
"NAME": "ID",
"TYPE": "Text"
},
{
"ID": ObjectId("51fd09a68b080ee40c0064db"),
"NAME": "Title",
"TYPE": "Text"
},
{
"ID": ObjectId("51fd09b28b080ee40c004d31"),
"NAME": "Start Date",
"TYPE": "Date"
},
{
"ID": ObjectId("51fd09c28b080ee40c007f2e"),
"NAME": "Long Description",
"TYPE": "Memo"
}
],
"name": "TODB",
"updated": "Wed Jul 31 2013 08:20:17 GMT-0400 (Eastern Daylight Time)"
}
我可以轻松获取“name”和“updated”的值,但无法弄清如何访问“fields”数组。
迄今为止的代码:
{
MongoServer mongo = MongoServer.Create();
mongo.Connect();
var db = mongo.GetDatabase("forms");
mongo.RequestStart(db);
var collection = db.GetCollection("forms");
var query = new QueryDocument("name",
"TODB");
mongo.Disconnect();
}
@foreach(BsonDocument item in collection.Find(query))
{
@item.GetElement("name").Value
@item.GetElement("_id").Value
}
我能够访问名称和 _id,只是无法访问任何子文档的值。
非常感谢您提前的帮助! 在弄清楚阅读之后,我还要写入数据....