【发布时间】: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。
我应该怎么做?
谢谢!
【问题讨论】: