小试牛刀Mongodb

/*
 * 由SharpDevelop创建。
 * 用户: Administrator
 * 日期: 2015/12/27
 * 时间: 13:03
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System.Drawing;

namespace mongotest
{
	class Program
	{	/// <summary>
        /// 数据库连接
         /// </summary>
        private const string conn = "mongodb://127.0.0.1:27017";
        /// <summary>
        /// 指定的数据库
         /// </summary>
        private const string dbName = "mongodb_name";
        /// <summary>
        /// 指定的表
         /// </summary>
        private const string tbName = "table_text";
		static void  sss(){
			
		
     //创建数据连接
         MongoServer server = MongoServer.Create(conn);
            //获取指定数据库
         MongoDatabase db = server.GetDatabase(dbName);
            //获取表
         MongoCollection col = db.GetCollection(tbName);
         
		}
         
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="articleId">文章ID</param>
        /// <param name="channelId">频道ID</param>
        /// <returns></returns>
        public static void Add(User t)
        {
            //创建数据连接
            MongoServer server = MongoServer.Create(conn);
            //获取指定数据库
            MongoDatabase db = server.GetDatabase(dbName);
            //获取表
            MongoCollection col = db.GetCollection(tbName);
            //插入
            col.Insert(t);

        }
        public class User{
        	public User()
        	{
        		Id = Guid.NewGuid().ToString("N");
        		Nation = "中国";
        		Detail = new Detail();
        	}
        	public virtual string Id{get;set;}
        	public virtual string Name{get;set;}
        	public virtual string Sex{get;set;}
        	public virtual DateTime Birth{get;set;}
        	public virtual string Nation{get;set;}
        	public virtual Detail Detail{get;set;}
        	public virtual Image Photo{get;set;}
			public override string ToString()
			{
				return string.Format("[User Id={0}, Name={1}, Sex={2}, Birth={3}, Nation={4}, Detail={5}]", Id, Name, Sex, Birth, Nation, Detail);
			}



        }
        public static void tQuery()
{
    MongoServer server = MongoServer.Create(conn);
            //获取指定数据库
            MongoDatabase db = server.GetDatabase(dbName);
                        //获取表
            MongoCollection<User> col = db.GetCollection<User>(tbName);
    //定义获取“Name”值为“xumingxiang”的查询条件
 var query = new QueryDocument { { "Nation", "中国" } };
          
    //查询全部集合里的数据
 var result1 = col.FindAllAs<User>();
 
  
  
    //查询指定查询条件的第一条数据,查询条件可缺省。
 var result2 = col.FindOneAs<User>();
    //查询指定查询条件的全部数据
 var result3 = col.FindAs<User>(query);
 
 foreach (User t in result3)
            {
            	Console.WriteLine(t);            	
            }
}

        /// <summary>
/// 用户详细资料
/// </summary>
public class Detail
{
  public string Address { get; set; }
  public int Age { get; set; }
  public string Email { get; set; }
  
  public Detail()
  {
  	Address = "保留";
  }
  
			public override string ToString()
			{
				return string.Format("[Detail Address={0}, Age={1}, Email={2}]", Address, Age, Email);
			}

}

		public static void Main(string[] args)
		{
			sss();
			
		 MongoServer server = MongoServer.Create(conn);
            //获取指定数据库
            MongoDatabase db = server.GetDatabase(dbName);
                        //获取表
            MongoCollection<User> col = db.GetCollection<User>(tbName);
            
            Console.WriteLine(col.Count());
            
    //定义获取“Name”值为“xumingxiang”的查询条件
 var query = new QueryDocument { { "Name", "李小龙" } };
  var result2 = col.FindOneAs<User>(query);
  
  var r=col.FindAs<User>(Query.Exists("Photo"));
  Console.WriteLine(r.Count());
 
//			Add(new User(){
//			    Name = "杨晓光",
//			    Photo = Image.FromFile("C:\\1.jpg")});
		
		
		
			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
		}
		/// <summary>
        /// 根据ObjectID 查询
        /// </summary>
        public static User SelectOne(string objId)
        {
            //创建数据连接
            MongoServer server = MongoServer.Create(conn);
            //获取指定数据库
            MongoDatabase db = server.GetDatabase(dbName);
            //获取表
            MongoCollection<User> col = db.GetCollection<User>(tbName);
            //条件查询            
            return col.FindOne(Query.EQ("_id", new ObjectId(objId)));
        }
		 public static void SelectAll()
        {
            List<User> list = new List<User>();

            //创建数据连接
            MongoServer server = MongoServer.Create(conn);
            //获取指定数据库
            MongoDatabase db = server.GetDatabase(dbName);
            //获取表
            MongoCollection<User> col = db.GetCollection<User>(tbName);
            //查询全部
            list.AddRange(col.FindAll());

            //--------------------------------
            foreach (User t in col.FindAll())
            {
            	Console.WriteLine(t);
            	t.Name = DateTime.Now.ToString();
            	
            }
        }
		 
		 /// <summary>
        /// 根据ObjectID 删除
        /// </summary>
        /// <param name="objId"></param>
        public static void Delete(string objId)
        {
            //创建数据连接
            MongoServer server = MongoServer.Create(conn);
            //获取指定数据库
            MongoDatabase db = server.GetDatabase(dbName);
            //获取表
            MongoCollection<User> col = db.GetCollection<User>(tbName);

            IMongoQuery query = Query.EQ("_id", new ObjectId(objId));

            col.Remove(query);
        }
        
		  /// <summary>
        /// 根据ObjectID 修改
        /// </summary>
        public static void Update(User t)
        {
            //创建数据连接
            MongoServer server = MongoServer.Create(conn);
            //获取指定数据库
            MongoDatabase db = server.GetDatabase(dbName);
            //获取表
            MongoCollection<User> col = db.GetCollection<User>(tbName);

            BsonDocument bd = BsonExtensionMethods.ToBsonDocument(t);

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

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

        }
	}
}


你可能感兴趣的:(小试牛刀Mongodb)