C#实现对mongoDB的简单增删查改

首先添加所需要驱动包(可通过nuget获得)

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

一、设置配置信息

  //连接信息

        static string conn = "mongodb://localhost";

        static string database = "test";

        static string collection = "person";



        static MongoServer mongodb = MongoServer.Create(conn);//连接数据库

        static MongoDatabase db = mongodb.GetDatabase(database);//选择数据库名

        static MongoCollection mc = db.GetCollection(collection);//选择集合,相当于表

二、插入数据库

public static void Insert(Person p)

{

  mc.Insert(p);

}



public static void Insert(BsonDocument b)  

{

  mc.Insert(b);

}

三、更新数据库

     public static void Update(Person p)

        {

            BsonDocument bd = BsonExtensionMethods.ToBsonDocument(p);

            IMongoQuery query = Query.EQ("_id", p._id);

            mc.Update(query, new UpdateDocument(bd));

        }



        public static void Update(QueryDocument q,UpdateDocument u)

        {

            mc.Update(q, u);

        }

 

四、删除某条记录

 public static void Delete(int id)

 {

     mc.Remove(Query.EQ("_id", id));

 }

五、查询数据库

        public static Person Find(int id)

        {

            return mc.FindOneAs<Person>(Query.EQ("_id", id));

        }



        public static MongoCursor<Person> FindAll()

        {

            return mc.FindAllAs<Person>();

        }



        public static MongoCursor<Person> Select(QueryDocument q)

        {

            return mc.FindAs<Person>(q);

        }

六、统计数据个数

 public static long Count(QueryDocument q)

  {

       return mc.Count(q);

 }

七、排序和分页

   public static MongoCursor<Person> SkipAndLimit(int a, int b)

        {

            return mc.FindAllAs<Person>().SetSkip(a).SetLimit(b);

        }

 

八、应用与示例

   static void Main(string[] args)

        {

            mongodb.Connect();



            //var person = Find(124);

            //Console.WriteLine(person.Name);



            //person.Name = "guizhu";

            //Update(person);

            //Console.WriteLine(person.Name);



            var query = new QueryDocument { { "_id",13} };

            var update = new UpdateDocument { { "$set", new QueryDocument { { "PassWord", "aaaaa" } } } };

            Update(query,update);



            //对象插入

            //Person p = new Person { _id = 12, Name = "hello", PassWord = "4444" };

            //Insert(p);



            //BsonDocument 插入

            //BsonDocument b = new BsonDocument();

            //b.Add("_id", 13);

            //b.Add("Name", "world");

            //b.Add("PassWord", "6666");

            //Insert(b);



            //var p= FindAll();

            //foreach (var person in p)

            //{

            //    Console.WriteLine(person.Name);

            //}



            //QueryDocument query = new QueryDocument();

            //BsonDocument b = new BsonDocument();

            //b.Add("$gte", 123);

            //b.Add("$lt", 125);

            //query.Add("_id", b);

            //var p=Select(query);

            //foreach (var person in p)

            //{

            //    Console.WriteLine(person.Name);

            //}



            //Console.WriteLine(Count(query));



            //FieldsDocument f = new FieldsDocument();

            //f.Add("Name", 1);

            //MongoCursor<Person> m = mc.FindAs<Person>(query).SetFields(f);

            //foreach (var person in p)

            //{

            //    Console.WriteLine(person.Name);

            //}





            //SortByDocument s = new SortByDocument();

            //s.Add("_id", 1);//-1=DESC

            //var p = Sort(s);

            //foreach (var person in p)

            //{

            //    Console.WriteLine(person.Name);

            //}



            //var p = SkipAndLimit(1, 3);

            //foreach (var person in p)

            //{

            //    Console.WriteLine(person.Name);

            //}

            

            //Delete(12);

            Console.WriteLine("完成");

            Console.ReadLine();

        }

 

  class Person

    {

        public int _id;

        public string Name;

        public string PassWord;



    }

源码下载地址:链接:http://pan.baidu.com/s/1c0tlXZi 密码:1es5

你可能感兴趣的:(mongodb)