【发布时间】:2014-11-24 07:24:55
【问题描述】:
我有一个 mongodb 集合“用户”,它以以下格式存储文档 -
{
"_id" : ObjectId("53fe7ae0ef038fee879263d5"),
"username" : "John"
"status" : "online",
"profile" : [
{
"name" : "John Stuart",
"age" : "23",
"gender" : "male"
}
]
}
我在 C#.NET 中使用以下函数将集合中的文档存储到 BsonDocuments 列表中 -
public static List<BsonDocument> LoadDataByWhere (string table, string whereClause)
{
// Here table is the collection name and whereClause is the mongodb query
var collection = db.GetCollection (table);
QueryDocument whereDoc = new QueryDocument(BsonDocument.Parse(whereClause));
var resultSet = collection.Find (whereDoc);
List<BsonDocument> docs = resultSet.ToList();
if (resultSet.Count() > 0) {
foreach(BsonDocument doc in docs)
{
doc.Set("_id", doc.GetElement("_id").ToString().Split('=')[1]);
}
return docs;
}
else {
return null;
}
}
假设我将返回的列表存储在列表中。我可以使用 -
List<string> username = new List<string>();
foreach(BsonDocument item in list)
{
username.Add(Convert.ToString(list.getElement("username").Value));
}
但是如何使用类似于上述方法的方法在 C# 中获取名称、年龄和性别等数组元素值?
【问题讨论】: