如果我有一个BSonDocument rappresenting like this
{
"name" : "hello"
"subdoc": {
"name": "bye"
}
}
我可以使用BSonDocument item.GetElement(“name”)轻松获得“hello”,但我怎样才能获得“bye”?
答案 0 :(得分:0)
以下是我发现使用MongoDB和C#最简单的方法。我创建了一个易于序列化的类,并使用Mongo驱动程序的LINQ功能。希望这可以帮助。
var databaseClient = new MongoClient(DatabaseConnectionString);
var server = databaseClient.GetServer();
var database = server.GetDatabase("YourDatabase");
var collection = database.GetCollection<YourObject>("YourObjects");
var subdoc = collection.AsQueryable().First(o => o.name == "hello").subdoc;
var subdocName = subdoc.name;
public class YourObject
{
public string name;
public OtherObject subdoc;
}
public class OtherObject
{
public string name;
}