0

我想插入一条有孩子的记录,然后用c#将写入的内容显示在屏幕上。这是我到目前为止所拥有的:

    MongoCollection<BsonDocument> house= building.GetCollection<BsonDocument>("house");
    BsonDocument rooms= new BsonDocument {
                { "roomName", name},
                { "location",  <--child array here: 1stfloor, 2ndlfloor, topfloor.
                { "roomID", guidstring}
                };

    house.Insert(rooms);
4

1 回答 1

0

你的意思是为了调试目的?为此,您可以将文档转换为 JSON 字符串:

Console.WriteLine(rooms.ToJson());

您还可以使用 mongo shell 查看您的文档。运行 mongo shell,然后键入:

> use buildings // or whatever your database name is
> db.house.find()
... your documents displayed here
> 

如果您的集合有很多文档,您可能希望包含某种查询来缩小显示的文档范围。

您还应该考虑使用 C# 类定义域模型,并让驱动程序为您将它们与 BSON 文档相互转换。

于 2012-04-17T21:30:13.380 回答