【问题标题】:Mongodb C# and SELECT nested valueMongodb C# 和 SELECT 嵌套值
【发布时间】:2018-08-20 22:28:31
【问题描述】:

我正在尝试使用一些 C# 代码在 mongodb 中进行简单的选择:

   "_id" : ObjectId("5aa04a34294f9c8f6c0ead85"), 
"Categorie" : "smartphone", 
"Plafond" : 1000.0, 
"SousCategorie" : [
    {
        "SousCategorieLibelle" : " 100-300 ", 
        "ValeurMin" : 100.0, 
        "ValeurMax" : 300.0, 
        "GarantieTarif" : [
            {
                "GarantieCode" : "VOL", 
                "GarantieLibelle" : "Vol", 
                "HT" : 0.09, 
                "Taxe" : 0.01, 
                "TTC" : 0.1, 
                "Franchise" : 50.0
            }, 
            {
                "GarantieCode" : "BOD", 
                "GarantieLibelle" : "Casse et Oxydation", 
                "HT" : 0.06, 
                "Taxe" : 0.01, 
                "TTC" : 0.07, 
                "Franchise" : 50.0
            }, 
            {
                "GarantieCode" : "CAT", 
                "GarantieLibelle" : "Catastrophes naturelles", 
                "HT" : 0.01, 
                "Taxe" : 0.01, 
                "TTC" : 0.02, 
                "Franchise" : 190.0
            }, 
            {
                "GarantieCode" : "CTEC", 
                "GarantieLibelle" : "Catastrophes technologiques", 
                "HT" : 0.0, 
                "Taxe" : 0.0, 
                "TTC" : 0.0, 
                "Franchise" : 50.0
            }
        ]
    }
], 
"Frais" : 0.02, 
"Commission" : 0.02

在 C# 中使用此代码:

 protected async void ddlcategorie_SelectedIndexChanged(object sender, EventArgs e)
    {
        IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("tarifs");

        BsonDocument filter = new BsonDocument();
        filter.Add("Categorie", "smartphone");

        using (var cursor = await collection.FindAsync(filter))
        {
            while (await cursor.MoveNextAsync())
            {
                var batch = cursor.Current;
                foreach (BsonDocument document in batch)
                {                       
                    txtValeur.Text += document["SousCategorie"]["SousCategorieLibelle"];
                 //   txtValeur.Text += document[1][2];
                }
            }
        }

    }

我总是得到一个错误:

System.NotSupportedException:'BsonArray 不支持索引 名称(只有 BsonDocument 有)。'

但如果我问document.GetType().ToString(); 我会得到 MongoDB.Bson.BsonDocument。

我应该怎么做?

谢谢!

【问题讨论】:

    标签: c# mongodb select nested


    【解决方案1】:

    问题是属性"SousCategorie"是一个数组(:之后的第一个字符是[),所以在索引document["SousCategorie"]时是正确的但返回BsonArray。据我了解,它是一个元素数组,因此您可以获取第一个元素(使用索引 0 或 First()),然后访问所需的属性

    可能下面的代码会起作用,但我没有测试它:

    foreach(var category in document["SousCategorie"])
    {
       Console.WriteLine(category["SousCategorieLibelle"));
    }
    

    【讨论】:

    • 我的查询返回超过 1 个结果(正好 3 个)对于 1 个类别(“智能手机”)我想获得这 3 个 SousCategorie.SousCategorieLibelle(100-300、300-500、500-1000)
    • 所以遍历它们并获取所有它们。您可以使用 foreach 或 Select
    • 我正在尝试,但我不知道如何重新获取嵌套项
    【解决方案2】:
      protected async void ddlcategorie_SelectedIndexChanged(object sender, EventArgs e)
    {
        IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("tarifs");
    
        BsonDocument filter = new BsonDocument();
        BsonDocument projection = new BsonDocument();
        projection.Add("Categorie", 1);
        projection.Add("SousCategorie.ValeurMin", 1);
        projection.Add("SousCategorie.SousCategorieLibelle", 1);
    
        var options = new FindOptions<BsonDocument>()
        {
            Projection = projection
        };
    
        using (var cursor = await collection.FindAsync(filter, options))
        {
            while (await cursor.MoveNextAsync())
            {
                var batch = cursor.Current;
                foreach (BsonDocument document in batch)
                {
                    Console.WriteLine(document.ToJson());
                }
            }
        }
    }
    

    我想获得“SousCategorie.SousCategorieLibelle”的值

    >? document["SousCategorie"][0].ToString()
    "{ \"SousCategorieLibelle\" : \" 100-300 \", \"ValeurMin\" : 100.0 }"
    >? document["SousCategorie"][0]
    {{ "SousCategorieLibelle" : " 100-300 ", "ValeurMin" : 100.0 }}
    
    
    >? document["SousCategorie.SousCategorieLibelle"].ToString()
    'document["SousCategorie.SousCategorieLibelle"].ToString()' a levé une exception de type 'System.Collections.Generic.KeyNotFoundException'
    

    我不明白如何才能更深入地了解我的数据库

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2015-07-30
      • 1970-01-01
      相关资源
      最近更新 更多