将 BSON 数组添加到 MongoDB 中的 BsonDocument

发布于 2024-11-13 12:15:48 字数 346 浏览 5 评论 0原文

如何使用 C# 驱动程序将 BsonArray 添加到 MongoDB 中的 BsonDocument?我想要这样的结果

{ 
    author: 'joe',
    title : 'Yet another blog post',
    text : 'Here is the text...',
    tags : [ 'example', 'joe' ],
    comments : [ { author: 'jim', comment: 'I disagree' },
                 { author: 'nancy', comment: 'Good post' }
    ]
} 

How can I add BsonArray to BsonDocument in MongoDB using a C# driver? I want a result something like this

{ 
    author: 'joe',
    title : 'Yet another blog post',
    text : 'Here is the text...',
    tags : [ 'example', 'joe' ],
    comments : [ { author: 'jim', comment: 'I disagree' },
                 { author: 'nancy', comment: 'Good post' }
    ]
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追我者格杀勿论 2024-11-20 12:15:48

您可以使用以下语句在 C# 中创建上述文档:

var document = new BsonDocument {
    { "author", "joe" },
    { "title", "yet another blog post" },
    { "text", "here is the text..." },
    { "tags", new BsonArray { "example", "joe" } },
    { "comments", new BsonArray {
        new BsonDocument { { "author", "jim" }, { "comment", "I disagree" } },
        new BsonDocument { { "author", "nancy" }, { "comment", "Good post" } }
    }}
};

您可以使用以下命令测试是否产生了正确的结果:

var json = document.ToJson();

You can create the above document in C# with the following statement:

var document = new BsonDocument {
    { "author", "joe" },
    { "title", "yet another blog post" },
    { "text", "here is the text..." },
    { "tags", new BsonArray { "example", "joe" } },
    { "comments", new BsonArray {
        new BsonDocument { { "author", "jim" }, { "comment", "I disagree" } },
        new BsonDocument { { "author", "nancy" }, { "comment", "Good post" } }
    }}
};

You can test whether you produced the correct result with:

var json = document.ToJson();
摘星┃星的人 2024-11-20 12:15:48

您还可以在 BsonDocument 已经存在之后添加数组,如下所示:

BsonDocument  doc = new BsonDocument {
    { "author", "joe" },
        { "title", "yet another blog post" },
     { "text", "here is the text..." }
};

BsonArray  array1 = new BsonArray {
        "example", "joe"
    };


BsonArray  array2 = new BsonArray {
        new BsonDocument { { "author", "jim" }, { "comment", "I disagree" } },
        new BsonDocument { { "author", "nancy" }, { "comment", "Good post" } }
    };


doc.Add("tags", array1);
doc.Add("comments", array2);

you can also add the array after the BsonDocument already exists, like this:

BsonDocument  doc = new BsonDocument {
    { "author", "joe" },
        { "title", "yet another blog post" },
     { "text", "here is the text..." }
};

BsonArray  array1 = new BsonArray {
        "example", "joe"
    };


BsonArray  array2 = new BsonArray {
        new BsonDocument { { "author", "jim" }, { "comment", "I disagree" } },
        new BsonDocument { { "author", "nancy" }, { "comment", "Good post" } }
    };


doc.Add("tags", array1);
doc.Add("comments", array2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文