7

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?

1 Answer 1

9

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; }
 }
Sign up to request clarification or add additional context in comments.

3 Comments

That would work with sturct too, right? Not only class, but struct as well.
Hey @AndrewOrsich would you mind looking at my question. It is very similar to the answer you provided above. I just cannot seem to make it work still. stackoverflow.com/questions/24089566/…
how to find channels.name="English"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.