• C# Mongodb基础应用


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MongoDB.Driver;
    
    namespace TalentCloud.Base.MongodbHelper
    {
        public class MongodbHelper
        {
    
            public void Demo() {
                MongodbHelper MongodbHelper = new MongodbHelper();
                UsersInfo UsersInfo = new UsersInfo();
                UsersInfo.Name = "ads";
                UsersInfo.Sex = "男";
                MongodbHelper.Insert<UsersInfo>(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo", UsersInfo);
                List<UsersInfo> aa = MongodbHelper.Query<UsersInfo>(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo");
    
                MongodbHelper.Update(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo", MongodbHelper.NewFilter("Name", "ssn2"), MongodbHelper.NewUpdate("Name", "ssn3"));
    
                List<UsersInfo> aa2 = MongodbHelper.Query<UsersInfo>(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo", MongodbHelper.NewFilter("Name", "ssn2"));
    
                List<UsersInfo> aa1 = MongodbHelper.Query<UsersInfo>(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo");
    
                MongodbHelper.Delete(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo", MongodbHelper.NewFilter("Name", "ssn"));
            }
            
            /// <summary>
            /// 插入数据
            /// </summary>
            /// <typeparam name="T">泛型数据</typeparam>
            /// <param name="dbName">插入的数据库</param>
            /// <param name="ObjName">插入的对象名,类似表名</param>
            /// <param name="data">数据对象</param>
            public void Insert<T>(string dbName, string ObjName, T data)
            {
                MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
                //获得Users集合,如果数据库中没有,先新建一个 
                MongoCollection MongoCollection = db.GetCollection(ObjName);
                //执行插入操作
                MongoCollection.Insert<T>(data);
            }
    
            /// <summary>
            /// 更新数据
            /// </summary>
            /// <param name="dbName"></param>
            /// <param name="ObjName"></param>
            /// <param name="query">    定义获取“Name”值为“xumingxiang”的查询条件 var query = new QueryDocument { { "Name", "xumingxiang" } };</param>
            /// <param name="update">  定义更新文档   var update = new UpdateDocument { { "$set", new QueryDocument { { "Sex", "wowen" } } } };</param>
            /// 注意:千万要注意查询条件和数据库内的字段类型保持一致,否则将无法查找到数据。如:如果Sex是整型,一点要记得将值转换为整型。
            public void Update(string dbName, string ObjName, QueryDocument query, UpdateDocument update)
            {
                MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
                //获取Users集合 
                MongoCollection MongoCollection = db.GetCollection(ObjName);
                //执行更新操作 
                MongoCollection.Update(query, update);
            }
    
            /// <summary>
            /// 删除数据
            /// </summary>
            /// <param name="dbName"></param>
            /// <param name="ObjName"></param>
            /// <param name="query"> 定义获取“Name”值为“xumingxiang”的查询条件  var query = new QueryDocument { { "Name", "xumingxiang" } };</param>
            public void Delete(string dbName, string ObjName, QueryDocument query)
            {
                MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
                //获取Users集合 
                MongoCollection MongoCollection = db.GetCollection(ObjName);
                //执行删除操作 
                MongoCollection.Remove(query);
            }
    
            /// <summary>
            /// 查询全部数据
            /// </summary>
            /// <param name="dbName">需要查询的库</param>
            public List<T> Query<T>(string dbName, string ObjName)
            {
    
                MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
                //获取Users集合 
                MongoCollection col = db.GetCollection(ObjName);
                //查询全部集合里的数据 
                return col.FindAllAs<T>().ToList();
                 
            }
    
            /// <summary>
            /// 查询指定查询条件的全部数据 
            /// </summary>
            /// <param name="dbName">需要查询的库</param>
            /// <param name="query">查询条件 var query = new QueryDocument { { "Name", "xumingxiang" } };</param>
            public List<T> Query<T>(string dbName, string ObjName, QueryDocument query)
            {
    
                MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
                //获取Users集合 
                MongoCollection col = db.GetCollection(ObjName);
                //查询指定查询条件的全部数据 
                return col.FindAs<T>(query).ToList(); ;
            }
    
            /// <summary>
            /// 查询指定查询条件的第一条数据
            /// </summary>
            /// <param name="dbName">需要查询的库</param>
            /// <param name="query">查询条件 var query = new QueryDocument { { "Name", "xumingxiang" } };</param>
            public T QueryOne<T>(string dbName, string ObjName, QueryDocument query)
            {
    
                MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
                //获取Users集合 
                MongoCollection col = db.GetCollection(ObjName);
                //查询指定查询条件的全部数据 
                return col.FindOneAs<T>(query); ;
            }
    
            /// <summary>
            /// 创建查询条件
            /// </summary>
            /// <returns></returns>
            public QueryDocument NewFilter(string key,string value) {
                return new QueryDocument { { key, value } };
            }
    
            /// <summary>
            /// 创建更新条件
            /// </summary>
            /// <returns></returns>
            public UpdateDocument NewUpdate(string key, string value)
            {
                return new UpdateDocument { { "$set", new QueryDocument { { key, value } } } };
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MongoDB.Bson;
    using MongoDB.Driver;
    using System.Configuration;
    
    namespace TalentCloud.Base.MongodbHelper
    {
       public class MongodbUtil
        {
    
           public static string MongodbConnection
           {
               get
               {
             //mongodb://127.0.0.1:27017
    return ConfigurationManager.AppSettings["MongodbConnection"].ToString(); } } /// <summary> /// 获取MongoClient /// </summary> /// <returns></returns> public static MongoServer GetMongoServer() { //创建数据库链接 return MongoServer.Create(MongodbConnection); } /// <summary> /// 获取MongoServer /// </summary> /// <returns></returns> public static MongoServer GetMongoServer(string Connection) { //创建数据库链接 return MongoServer.Create(Connection); } //获取DbBase public static MongoDatabase GetMongodbDatabase(string dbName) { MongoServer MongoServer = GetMongoServer(); //获得数据库cnblogs MongoDatabase DbBase = MongoServer.GetDatabase(dbName); return DbBase; } } }

    支持.net4.0

    https://files.cnblogs.com/files/yyyuguo/mongodbDll.zip  

  • 相关阅读:
    【名称解释】#001 IIS名词解释
    【C#】#102 发送邮件
    【C#】#101 导入导出Excel
    【C#】#100 调用摄像头
    【转】【Flex】#010 操作XML文件(E4X)
    【Flex教程】#009 As/typeof /instanceof /is的作用
    4 权限组件、频率、3组件总结
    1 request模块
    4 saltstack ssh
    03 深入远程执行:target目标、模块modules、返回returns
  • 原文地址:https://www.cnblogs.com/yyyuguo/p/8426994.html
Copyright © 2020-2023  润新知