public void Test()
        {
            //creamos la conexion y seleccionamos la base de datos
            var client = new MongoClient("mongodb://localhost:27017");
            MongoServer server = client.GetServer();
            MongoDatabase database = server.GetDatabase("testdb");
            MongoCollection<Person> collection = database.GetCollection<Person>("persons");
            collection.RemoveAll();

            //creamos un objeto persona
            var p = new Person {Age = 25, Name = "person Name", Cars=1,Childs = new List<Child>()};
            p.Childs.Add(new Child {Name = "child 1", Age = 12});
            p.Childs.Add(new Child {Name = "child 2", Age = 16});

            //lo guardamos
            var result = collection.Save(p);
            if (result.HasLastErrorMessage) throw new Exception(result.ErrorMessage);

            p = collection.FindOne();
            Console.WriteLine();
            Console.WriteLine(p.ToJson());

            //modificamos el objeto y lo enviamos entero
            p.Name = "person Name 2";
            p.Age = 35;
            p.Tags = new List<string>() {"A","B"};
            p.Childs[0].Age = 5;

            result = collection.Save(p);
            if (result.HasLastErrorMessage) throw new Exception(result.ErrorMessage);

            p = collection.FindOne();
            Console.WriteLine();
            Console.WriteLine(p.ToJson());

            //le modificamos el nombre y la edad
            collection.Update(Query.EQ("_id", p.Id), Update<Person>.Set(x=>x.Name, "Name2").Inc(x=>x.Age, 1));

            p = collection.FindOne();
            Console.WriteLine();
            Console.WriteLine(p.ToJson());

            //le anyadimos un hijo a los que tengan 37 años
            collection.Update(Query.EQ("Age", 36), Update.PushWrapped("Childs", new Child {Name = "child 3", Age = 3}),
                              UpdateFlags.Multi);

            p = collection.FindOne();
            Console.WriteLine();
            Console.WriteLine(p.ToJson());
        }
        public void Test()
        {
            //creamos la conexion y seleccionamos la base de datos
            var client = new MongoClient("mongodb://localhost:27017");
            var server = client.GetServer();
            var database = server.GetDatabase("testdb");
            var collection = database.GetCollection<Person>("persons");
            collection.RemoveAll();

            //Creamos datos
            var r = new Random();
            string[] names = { "Juan", "Antonio", "Pedro", "Maria", "Jordi", "Mario" };
            string[] apellidos = { "Gomez", "Perez" };

            for (int i = 1; i < 100; i++)
            {
                string apellido = apellidos[r.Next(0, 1)];
                //creamos un objeto persona
                var p = new Person { Age = r.Next(25, 55), Name = names[r.Next(0, 4)] + " " + apellido, Childs = new List<Child>(), Tags = new List<string>() {"A","B"}};
                p.Childs.Add(new Child { Name = names[r.Next(0, 4)] + " " + apellido, Age = r.Next(1, 12) });
                p.Childs.Add(new Child { Name = names[r.Next(0, 4)] + " " + apellido, Age = r.Next(1, 12) });

                //lo guardamos
                collection.Insert(p);
            }

            Console.WriteLine("Cuantas personas de cada Nombre hay");

            var operations = new[]{
                new BsonDocument {
                {
                    "$group", new BsonDocument{ { "_id" , "$Name" } , {"Total" , new BsonDocument{ {"$sum",1} } } }
                }
               }
            };

            var result = collection.Aggregate(operations);

            foreach (var document in result.ResultDocuments)
            {
                Console.WriteLine(document.ToJson());
            }

            Console.WriteLine();
            Console.WriteLine("Años Sumados de todas las personas x nombre");

            operations = new[]{
                new BsonDocument {
                {
                    "$group", new BsonDocument{ { "_id" , "$Name" } , {"Total" , new BsonDocument{ {"$sum","$Age"} } } }
                }
               }
            };

            result = collection.Aggregate(operations);

            foreach (var document in result.ResultDocuments)
            {
                Console.WriteLine(document.ToJson());
            }

            Console.WriteLine();
            Console.WriteLine("Cuantos hay y las edades unicas con edad >=40 y con hijos edad >= 4");

            string nameFilter = "Juan Gomez";

            operations = new[]{
                new BsonDocument{
                    {
                        "$match" , new BsonDocument
                                       {
                                           {"Age" , new BsonDocument{ {"$gte" , 40} } },
                                           {"Childs.Age" , new BsonDocument{ {"$gte" , 4} } }
                                       }
                    }
                },
                new BsonDocument {
                {
                    "$group", new BsonDocument{ { "_id" , "$Name" } , {"Total" , new BsonDocument{ {"$sum",1} } },
                              new BsonDocument("UniqueAges" , new BsonDocument( "$addToSet" , "$Age" )) }
                }
               },
                new BsonDocument("$sort" , new BsonDocument( "Total" , -1 )),
                new BsonDocument{
                    {
                        "$project", new BsonDocument{ {"_id",1}, {"Total",1},{"UniqueAges",1} }
                    }
                },
                new BsonDocument("$match" , new BsonDocument( "Total" , new BsonDocument{ {"$gte" , 10} } )),
                //new BsonDocument("$unwind" , "$UniqueAges")
            };

            result = collection.Aggregate(operations);

            foreach (var document in result.ResultDocuments)
            {
                Console.WriteLine(document.ToJson());
            }
        }
        public void Test()
        {
            //creamos la conexion y seleccionamos la base de datos
            var client = new MongoClient("mongodb://localhost:27017");
            var server = client.GetServer();
            var database = server.GetDatabase("testdb");
            var collection = database.GetCollection<Person>("persons");
            collection.Drop();

            //Creamos datos
            var r = new Random();
            string[] names = {"Juan", "Antonio", "Pedro", "Maria", "Jordi", "Mario"};
            string[] apellidos = {"Gomez","Perez"};

            var personsToInsert = new List<Person>();
            for(int i=1; i < 100; i++)
            {
                string apellido = apellidos[r.Next(0, 1)];
                //creamos un objeto persona
                var p = new Person { Age = r.Next(25, 55), Name = names[r.Next(0,4)] + " " + apellido, Childs = new List<Child>() };
                p.Childs.Add(new Child { Name =  names[r.Next(0,4)] + " " + apellido, Age = r.Next(1, 12) });
                p.Childs.Add(new Child { Name =  names[r.Next(0,4)] + " " + apellido, Age = r.Next(1, 12) });
                personsToInsert.Add(p);
            }

            //Insertamos en Bach
            collection.InsertBatch(personsToInsert);

            Console.WriteLine("All person");

            var persons = collection.FindAll();
            Console.WriteLine("Explain: " + persons.Explain().ToJson());
            foreach (Person person in persons)
            {
                Console.WriteLine(person.ToJson());
            }

            Console.WriteLine();
            Console.WriteLine("Personas con age = 25 sin key");

            persons = collection.Find(Query<Person>.EQ(p=>p.Age, 25));
            Console.WriteLine("Explain: " + persons.Explain().ToJson());
            foreach (Person person in persons)
            {
                Console.WriteLine(person.ToJson());
            }

            Console.WriteLine();
            Console.WriteLine("Personas con age = 25 con key");

            collection.EnsureIndex("Age");
            persons = collection.Find(Query.EQ("Age", 25));
            Console.WriteLine("Explain: " + persons.Explain().ToJson());
            foreach (Person person in persons)
            {
                Console.WriteLine(person.ToJson());
            }

            Console.WriteLine();
            Console.WriteLine("Personas con age = 25 o = 26 que se llamen Juan");

            persons = collection.Find(Query.And(Query.Or(Query.EQ("Age", 25), Query.EQ("Age", 26)),Query.Matches("Name",new BsonRegularExpression("Juan"))));
            Console.WriteLine("Explain: " + persons.Explain().ToJson());
            foreach (Person person in persons)
            {
                Console.WriteLine(person.ToJson());
            }

            Console.WriteLine();
            Console.WriteLine("Personas con age >= 30 y <= 40 ordenado por age");

            persons = collection.Find(Query.And(Query.GTE("Age", 30),Query.LTE("Age",40))).SetSortOrder("Age");
            Console.WriteLine("Explain: " + persons.Explain().ToJson());
            foreach (Person person in persons)
            {
                Console.WriteLine(person.ToJson());
            }

            Console.WriteLine();
            Console.WriteLine("Personas con age = 30 y con child age = 5 ordenado por age");
            persons = collection.Find(Query.And(Query.EQ("Age", 30), Query.EQ("Childs.Age", 5))).SetSortOrder("Age");
            Console.WriteLine("Explain: " + persons.Explain().ToJson());
            foreach (Person person in persons)
            {
                Console.WriteLine(person.ToJson());
            }

            Console.WriteLine();
            Console.WriteLine("Personas con Childs age = 5, solo devolvemos el campo childs");
            persons = collection.Find(Query.EQ("Childs.Age", 5)).SetFields("Childs");
            Console.WriteLine("Explain: " + persons.Explain().ToJson());
            foreach (Person person in persons)
            {
                Console.WriteLine(person.ToJson());
            }
        }