如何使用官方 c# 驱动程序创建以下文档?
{
"name": "John Doe",
"classess": [
{
"classname": "Class1"
}
{
"classname": "Class2"
}
]
}
下面的代码不起作用
string cs = "mongodb://localhost";
MongoServer server = MongoServer.Create(cs);
MongoDatabase test = server.GetDatabase("test");
MongoCollection<BsonDocument> students = test.GetCollection("students");
BsonDocument doc = new BsonDocument();
doc.Add(new BsonElement("name", "John doe"));
//Create the list
List<BsonDocument> classes = new List<BsonDocument>();
classes.Add(new BsonDocument(new BsonElement("classname","Test1")));
classes.Add(new BsonDocument(new BsonElement("classname","Test2")));
出于显而易见的原因,以下行将引发错误。这样做的正确方法是什么?
doc.Add(new BsonElement("classess",classes));
students.Insert(doc);
谢谢你。