简体   繁体   English

mongodb c# 如何使用 BSON 文档

[英]mongodb c# how to work with BSON document

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.我花了很多时间寻找答案......这在 PHP 中很容易,但我无法将它放在 C# 中(我是 C# 和 mongo 的新手......)我正在尝试迭代存储文件的所有级别。 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.我访问“名称”和“更新”没有问题,但不知道如何访问“字段”数组。

Code so far :到目前为止的代码:

{
    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
}

Again, I am able to access the name and _id just not any of the sub document values.同样,我可以访问 name 和 _id 而不是任何子文档值。

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

You can often use the string indexer ["name-of-property"] to walk through the fields and sub document fields.您通常可以使用字符串索引器["name-of-property"]来遍历字段和子文档字段。 Then, using the AsXYZ properties to cast the field value to a particular type as shown above.然后,使用AsXYZ属性将字段值转换为特定类型,如上所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM