How to retrieve values from an array in MongoDB using C# -
below in code retrieves elements in form of bsonarray. want fetch numeric value array , use value calculate sum.
var fields = "secondary.amount"; foreach (var document in collection.findallas<bsondocument>().setfields(fields)) { foreach (string name in document.names) { bsonelement element = document.getelement(name); console.writeline("{0}", element.value); } }
i tried converting bson element int64, int32, double , use numeric value addition runtime error of not able cast bsonarray etc. have idea on how go this?
i figured out solution, making following changes , works now:
foreach (bsondocument nesteddocument in document["name"].asbsonarray) { total += convert.todouble(nesteddocument["amount"]); }
Comments
Post a Comment