mongodb c# 如何使用 BSON 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18068772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:12:50  来源:igfitidea点击:

mongodb c# how to work with BSON document

c#mongodbbson

提问by user2167261

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:

我花了很多时间寻找答案......这在 PHP 中很容易,但我无法将它放在 C# 中(我是 C# 和 mongo 的新手......)我正在尝试迭代存储文件的所有级别。该文件如下所示:

{
    "_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....

在此先感谢您的帮助!读完后,我也想写数据了....

采纳答案by WiredPrairie

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. Then, using the AsXYZproperties to cast the field value to a particular type as shown above.

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