1

Ok, so i'm new to mongodb databases. I created a small test class to fill a database with 'Country' objects. Now, i would like to extract that data in the database to a json file.. I looked everywhere on the internet and here on Stack, but just couldn't find the answer, please help...

public class Country
{
    public string Name { get; set; }
    public string Language { get; set; }
    public string Capital_City { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        try
        {
            var connectionString = "mongodb://localhost";
            var client = new MongoClient(connectionString);
            var server = client.GetServer();
            var database = server.GetDatabase("Countries");
            var collection = database.GetCollection<Country>("Countries");

            BsonClassMap.RegisterClassMap<Country>(x =>
                {
                    x.MapProperty(c => c.Name);
                    x.MapProperty(c => c.Language);
                    x.MapProperty(c => c.Capital_City);
                });
            hardCodedInfo(collection);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    static void hardCodedInfo(MongoCollection collection)
    {
        var tmp1 = new Country() { Capital_City = "Belgrade", Name = "Serbia", Language = "Serbian" };
        collection.Insert(tmp1);
        var tmp2 = new Country() { Capital_City = "Vienna", Name = "Austria", Language = "German" };
        collection.Insert(tmp2);
        var tmp3 = new Country() { Capital_City = "Beijing", Name = "China", Language = "Mandarin" };
        collection.Insert(tmp3);
        var tmp4 = new Country() { Capital_City = "Moscow", Name = "Russia", Language = "Russian" };
        collection.Insert(tmp4);
        var tmp5 = new Country() { Capital_City = "Tokio", Name = "Japan", Language = "Japanese" };
        collection.Insert(tmp5);
        var tmp6 = new Country() { Capital_City = "Brasilia", Name = "Brasil", Language = "Portugese" };
        collection.Insert(tmp6);
        var tmp7 = new Country() { Capital_City = "Paris", Name = "France", Language = "French" };
        collection.Insert(tmp7);
        var tmp8 = new Country() { Capital_City = "Antananarivo", Name = "Madagascar", Language = "Malagasy" };
        collection.Insert(tmp8);
        var tmp9 = new Country() { Capital_City = "Andorra la Vella", Name = "Andorra", Language = "Catalan" };
        collection.Insert(tmp9);
        var tmp10 = new Country() { Capital_City = "Ulan Bator", Name = "Mongolia", Language = "Mongolian" };
        collection.Insert(tmp10);
    }
}
Nikola.Lukovic
  • 968
  • 1
  • 12
  • 32
  • There are thousands of pages dealing with serializing C# objects into JSON. So, I'm not sure what you're looking for. http://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx – WiredPrairie Apr 19 '14 at 12:25
  • possible duplicate of [Turn C# object into a JSON string in .NET 4](http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4) – WiredPrairie Apr 19 '14 at 12:26

0 Answers0