MongoDB:如何在 C# 中加载带有嵌套数组的集合?

发布于 2024-10-19 11:23:30 字数 741 浏览 3 评论 0原文

我有一个名为“服务器”的集合,其中包含以下文档。

{
    name: "West",
    ip: "123.123.123.123",
    channels:
    [
        {
            name: "English",
            port: "1234",
            status: "0"
        },
        {
            name: "Spanish",
            port: "1235",
            status: "0"
        },
        {
            name: "German",
            port: "1236",
            status: "0"
        }
    ]
},
{
    name: "East",
    ip: "122.122.122.122",
    channels:
    [
        {
            name: "English",
            port: "1234",
            status: "0"
        },
        {
            name: "French",
            port: "1235",
            status: "0"
        }
    ]
}

我如何使用 C# 使用结构从 MongoDB 中选择它?

I have collection called "servers" with following documents.

{
    name: "West",
    ip: "123.123.123.123",
    channels:
    [
        {
            name: "English",
            port: "1234",
            status: "0"
        },
        {
            name: "Spanish",
            port: "1235",
            status: "0"
        },
        {
            name: "German",
            port: "1236",
            status: "0"
        }
    ]
},
{
    name: "East",
    ip: "122.122.122.122",
    channels:
    [
        {
            name: "English",
            port: "1234",
            status: "0"
        },
        {
            name: "French",
            port: "1235",
            status: "0"
        }
    ]
}

How would I select that from MongoDB using C# using structures?

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

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

发布评论

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

评论(1

情未る 2024-10-26 11:23:30

如果您想要所有项目,您可以使用以下代码:

var server = MongoServer.Create("mongodb://localhost:27020");
var database = server.GetDatabase("someDb");

var servers = database.GetCollection<ServerItem>("servers");
servers.FindAllAs<ServerItem>();

但是如果您想要例如 name = west 的所有文档,则可以:

collection.FindAs<ServerItem>(Query.EQ("name","west"));

ServerItem:

 public class ServerItem
 {
   public string name { get; set; }

   public string ip { get; set; }

   public List<Channel> channels { get; set; }
 } 

 public class Channel
 {
   public string name { get; set; }

   public int port { get; set; }

   public int status { get; set; }
 }

If you want all items you can use follwoing code:

var server = MongoServer.Create("mongodb://localhost:27020");
var database = server.GetDatabase("someDb");

var servers = database.GetCollection<ServerItem>("servers");
servers.FindAllAs<ServerItem>();

But if you want for example all documents with name = west, than you can:

collection.FindAs<ServerItem>(Query.EQ("name","west"));

ServerItem:

 public class ServerItem
 {
   public string name { get; set; }

   public string ip { get; set; }

   public List<Channel> channels { get; set; }
 } 

 public class Channel
 {
   public string name { get; set; }

   public int port { get; set; }

   public int status { get; set; }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文