mongodb C#如何使用BSON文档 [英] mongodb c# how to work with BSON document

查看:360
本文介绍了mongodb C#如何使用BSON文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了很多时间寻找答案... 这在PHP中非常简单,但我无法在C#中将其组合在一起(我是C#和mongo的新手.) 我正在尝试遍历存储文档的所有级别.该文档如下所示:

I've spent MANY hours looking for the answer... This is very easy in PHP but I just can't put it together in C#(I'm new to C# and mongo...) I'm trying to iterate through all levels of a stored document. The document looks like this:

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

访问名称"和已更新"没有问题,但无法弄清楚如何访问字段"数组.

I have no problem accessing the "name" and "updated" but can't figure out how to access the "fields" array.

到目前为止的代码:

{
    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,但不能访问任何子文档值.

Again, I am able to access the name and _id just not any of the sub document values.

在此先感谢您的协助! 弄清楚阅读内容之后,我还将要写数据....

Thanks in advance for any assistance! After I get the reading figured out, I am also going to want to write data....

推荐答案

有几种方法,但这是一种:

There are a few ways, but here's one:

 // build some test data
 BsonArray dataFields = new BsonArray { new BsonDocument { 
     { "ID" , ObjectId.GenerateNewId()}, { "NAME", "ID"}, {"TYPE", "Text"} } };
 BsonDocument nested = new BsonDocument {
     { "name", "John Doe" },
     { "fields", dataFields },
     { "address", new BsonDocument {
             { "street", "123 Main St." },
             { "city", "Madison" },
             { "state", "WI" },
             { "zip", 53711}
         }
     }
 };
 // grab the address from the document,
 // subdocs as a BsonDocument
 var address = nested["address"].AsBsonDocument;
 Console.WriteLine(address["city"].AsString); 
 // or, jump straight to the value ...
 Console.WriteLine(nested["address"]["city"].AsString);
 // loop through the fields array
 var allFields = nested["fields"].AsBsonArray ;
 foreach (var fields in allFields)
 {
     // grab a few of the fields:
     Console.WriteLine("Name: {0}, Type: {1}", 
         fields["NAME"].AsString, fields["TYPE"].AsString);
 }

您通常可以使用字符串索引器["name-of-property"]遍历字段和子文档字段.然后,使用AsXYZ属性将字段值转换为特定类型,如上所示.

You can often use the string indexer ["name-of-property"] to walk through the fields and sub document fields. Then, using the AsXYZ properties to cast the field value to a particular type as shown above.

这篇关于mongodb C#如何使用BSON文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆