【问题标题】:mongodb c# how to work with BSON documentmongodb c#如何使用BSON文档
【发布时间】:2013-08-06 18:52:25
【问题描述】:

我花了很多时间寻找答案... 这在 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)"
}

访问“名称”和“更新”没有问题,但不知道如何访问“字段”数组。

到目前为止的代码:

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

提前感谢您的帮助! 读完之后,我也想写数据....

【问题讨论】:

    标签: c# mongodb bson


    【解决方案1】:

    有几种方法,但这里有一个:

     // 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 属性将字段值转换为特定类型,如上所示。

    【讨论】:

    • 你知道有没有更简单的东西?像 Key.Key.Key?如果你保存了面包屑,那么 ["key"]["key"]["key"].AsString 真的很糟糕
    • 我不确定在这种情况下您所说的面包屑是什么意思。该对象有一个索引器来按名称检索字段。除非您创建一个强类型类,否则您将无法获得您想要的体验。
    猜你喜欢
    • 2018-01-19
    • 1970-01-01
    • 2021-04-17
    • 2015-09-27
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多