Beispiel #1
0
        public void TestMongoDB()
        {
            var server = returnMongoServer();

            //get DB
            var database = server.GetDatabase("iStore");
            var collection = database.GetCollection<People>("people");

            //Find all
            var entity = collection.FindAll();
            var iList = entity.ToList();

            Console.WriteLine("Total b4 insert: {0}", iList.Count());
            foreach (var item in iList)
            {
                Console.WriteLine("### Name: {0} | Sex: {1}", item.name, item.sex);
            }

            //Insert data
            var people = new People { name = "Miki" };
            collection.Insert(people);
            //Find new data
            var idInset = people.Id;
            var query = Query.EQ("_id", idInset);
            people = collection.FindOne(query);
            Console.WriteLine("");
            Console.WriteLine("Insert new data id: {0}", idInset);
            Console.WriteLine("      info. name: {0} | Sex: {1}", people.name, people.sex);

            //update name for new data
            Console.WriteLine("");
            Console.Write("Change name from: {0}", people.name);
            people.name = "Fire Man";
            people.sex = "Male";
            collection.Save(people);
            people = collection.FindOne(query);
            Console.WriteLine(" to: {0} | {1}", people.name, people.sex);

            //update again by fn update(_id)
            var update = Update.Set("name", "Lucus");
            collection.Update(query, update);
            people = collection.FindOne(query);
            Console.WriteLine("--------- update again");
            Console.WriteLine("              name: {0}", people.name);

            Console.WriteLine("");
            Console.WriteLine("Remove: {0}", people.name);
            collection.Remove(query);
            iList = collection.FindAll().ToList();
            Console.WriteLine("Total : {0}", iList.Count());
        }
Beispiel #2
0
        public void TestMethod1()
        {
            var server = returnMongoServer();

            //get DB
            var database = server.GetDatabase("iStore");
            var collection = database.GetCollection("people");//.GetCollection<People>("people");

            //Find all
            var entity = collection.FindAllAs<People>();
            var iList = entity.ToList();

            foreach (var item in iList)
            {
                Console.WriteLine("data: {0}", item.age);
            }

            var p = new People();
            p.age = 10;
            p.name = "www";
            collection.Insert(p);
        }