BsonDocument包含一个数组。当BsonDocument包含在C#list中时,需要访问C#中的数组元素
我有一个mongodb集合“users”,它以以下格式存储文档-BsonDocument包含一个数组。当BsonDocument包含在C#list中时,需要访问C#中的数组元素,c#,arrays,mongodb,C#,Arrays,Mongodb,我有一个mongodb集合“users”,它以以下格式存储文档- { "_id" : ObjectId("53fe7ae0ef038fee879263d5"), "username" : "John" "status" : "online", "profile" : [ { "name" : "John Stuart", "age" : "23", "gender" : "m
{
"_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;
}
}
publicstaticlist-LoadDataByWhere(stringtable,stringwhere子句)
{
//这里table是集合名称,where子句是mongodb查询
var collection=db.GetCollection(表);
QueryDocument whereDoc=新的QueryDocument(BsonDocument.Parse(whereClause));
var resultSet=collection.Find(whereDoc);
List docs=resultSet.ToList();
如果(resultSet.Count()>0){
foreach(单据中的b单据)
{
doc.Set(“\u id”,doc.GetElement(“\u id”).ToString().Split(“=”)[1]);
}
退货单据;
}
否则{
返回null;
}
}
假设我存储列表中返回的列表。我可以用-
List<string> username = new List<string>();
foreach(BsonDocument item in list)
{
username.Add(Convert.ToString(list.getElement("username").Value));
}
List username=new List();
foreach(列表中的B文档项)
{
Add(Convert.ToString(list.getElement(“username”).Value));
}
但是,如何使用类似于上述方法的方法在C#中获取数组元素值,如姓名、年龄和性别?我建议将Mongo文档映射到某种形式的DTO。Mongo支持反序列化为对象图
public class User
{
[BsonId]
public ObjectId Id { get; set; }
public string username { get; set; }
public string status { get; set; }
public List<Profile> profile { get; set; }
}
public class Profile
{
public string name { get; set; }
public string age { get; set; }
public string gender { get; set; }
}
公共类用户
{
[BsonId]
公共对象Id{get;set;}
公共字符串用户名{get;set;}
公共字符串状态{get;set;}
公共列表配置文件{get;set;}
}
公共班级简介
{
公共字符串名称{get;set;}
公共字符串年龄{get;set;}
公共字符串{get;set;}
}
然后你可以像这样访问它
const string connectionString = "mongodb://localhost";
//// Get a thread-safe client object by using a connection string
var mongoClient = new MongoClient(connectionString);
//// Get a reference to a server object from the Mongo client object
var mongoServer = mongoClient.GetServer();
//// Get a reference to the database object
//// from the Mongo server object
const string databaseName = "mydatabase";
var db = mongoServer.GetDatabase(databaseName);
//// Get a reference to the collection object from the Mongo database object
//// The collection name is the type converted to lowercase + "s"
MongoCollection<T> mongoCollection = db.GetCollection<T>(typeof(T).Name.ToLower() + "s");
const string connectionString=”mongodb://localhost";
////使用连接字符串获取线程安全的客户端对象
var mongoClient=新的mongoClient(connectionString);
////从Mongo客户端对象获取对服务器对象的引用
var mongoServer=mongoClient.GetServer();
////获取对数据库对象的引用
////从Mongo服务器对象
const string databaseName=“mydatabase”;
var db=mongoServer.GetDatabase(databaseName);
////从Mongo数据库对象获取对集合对象的引用
////集合名称是转换为小写+s的类型
MongoCollection MongoCollection=db.GetCollection(typeof(T.Name.ToLower()+“s”);
因此,当您现在按id查询时,它将包含已填充的配置文件列表(如果存在)