当前位置: 首页 > 工具软件 > mongoHelper > 使用案例 >

MongoHelper类

公西翼
2023-12-01
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;
using MongoDB.Bson;
using System.Configuration;

namespace DBUtility
{
    /// <summary>
    /// MongoDB 的数据库访问帮助类
    /// </summary>
    public abstract class MongoHelper
    {
        //数据库url 集合名称

        private static readonly string MongoDatabaseUrl = ConfigurationManager.ConnectionStrings["MongoDbUrl"].ConnectionString;
        private static readonly string MongoCollectionName = ConfigurationManager.AppSettings["CollectionName"];

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="url">MongoUrl的String表示,该Url必须包含数据库名称</param>
        /// <param name="collection">集合名称</param>
        /// <param name="query">查询条件,null表示查询所有</param>
        /// <param name="fields">字段名称,null表示所有字段</param>
        /// <param name="sortBy">排序字段,null表示不进行排序</param>
        /// <returns>返回表示该查询结果的MongoCursor</returns>
        public static MongoCursor<BsonDocument> Find(
            string url,
            string collection,
            IMongoQuery query,
            IMongoFields fields,
            IMongoSortBy sortBy
            )
        {
            return Find(url, collection, query, fields, sortBy, 0, 0);
        }

        /// <summary>
        /// 查询,先排序,然后skip,接着limit
        /// </summary>
        /// <param name="url">MongoUrl的String表示,该Url必须包含数据库名称</param>
        /// <param name="collection">集合名称</param>
        /// <param name="query">查询条件,null表示查询所有</param>
        /// <param name="fields">字段名称,null表示所有字段</param>
        /// <param name="sortBy">排序字段,null表示不进行排序</param>
        /// <param name="skip">跳过的Document数</param>
        /// <param name="limit">返回的记录数,设置为 0 表示返回所有</param>
        /// <returns>返回表示该查询结果的MongoCursor</returns>
        public static MongoCursor<BsonDocument> Find(
            string url,
            string collection,
            IMongoQuery query,
            IMongoFields fields,
            IMongoSortBy sortBy,
            int skip,
            int limit
            )
        {
            MongoCursor<BsonDocument> retCursor = null;

            MongoCollection coll = GetCollection(url, collection);

            retCursor = coll.FindAs<BsonDocument>(query);

            if (fields != null)
                retCursor.SetFields(fields);
            if (sortBy != null)
                retCursor.SetSortOrder(sortBy);

            retCursor.Skip = skip;
            retCursor.Limit = limit;
            
            return retCursor;
        }

        /// <summary>
        /// 查询匹配查询结果的第一条记录
        /// </summary>
        /// <param name="url">MongoUrl的String表示,该Url必须包含数据库名称</param>
        /// <param name="collection">集合名称</param>
        /// <param name="query">查询条件,null表示查询所有</param>
        /// <returns>返回表示该查询结果的BsonDocument</returns>
        public static BsonDocument FindOne(
            string url,
            string collection,
            IMongoQuery query)
        {
            MongoCollection c = GetCollection(url, collection);

            return c.FindOneAs<BsonDocument>(query);
        }

        /// <summary>
        /// 插入一个文档
        /// </summary>
        /// <param name="url">MongoUrl的String表示</param>
        /// <param name="collection">集合名称</param>
        /// <param name="data">一个表示文档的BsonDocument对象</param>
        /// <returns>返回执行那个的SafeModeResult</returns>
        public static SafeModeResult Insert(
            string url,
            string collection,
            BsonDocument document
            )
        {
            SafeModeResult smr = new SafeModeResult();
            if (document != null)
                return GetCollection(url, collection).Insert<BsonDocument>(document, SafeMode.True);
            else
                throw new ArgumentNullException("参数 document 不能为空");
        }

        /// <summary>
        /// 更新多个匹配条件query的记录
        /// </summary>
        /// <param name="url">MongoUrl的String表示</param>
        /// <param name="collection">集合名称</param>
        /// <param name="query">查询条件,不能为null</param>
        /// <param name="update">更新字段及数据,相当于 set fieldName=newValue,不能为null</param>
        /// <returns>返回SafeModeResult</returns>
        public static SafeModeResult Update(
            string url,
            string collection,
            IMongoQuery query,
            IMongoUpdate update
            )
        {
            return MongoHelper.Update(url, collection, query, UpdateFlags.Multi, update);
        }

        /// <summary>
        /// 更新匹配条件query的文档,需要设定更新方式
        /// </summary>
        /// <param name="url">MongoUrl的String表示</param>
        /// <param name="collection">集合名称</param>
        /// <param name="query">查询条件,不能为null</param>
        /// <param name="update">更新字段及数据,相当于 set fieldName=newValue,不能为null</param>
        /// <param name="flags">更新方式</param>
        /// <returns>返回SafeModeResult</returns>
        public static SafeModeResult Update(
            string url,
            string collection,
            IMongoQuery query,
            UpdateFlags flags,
            IMongoUpdate update
            )
        {
            if (query == null)
                throw new ArgumentNullException("参数 query 不能为null");
            if(update == null)
                throw new ArgumentNullException("参数 update 不能为null");
            MongoCollection c = GetCollection(url, collection);
            return c.Update(query, update, flags, SafeMode.True);
        }

        /// <summary>
        /// 删除所有匹配query的记录,如果query为null则删除所有记录
        /// </summary>
        /// <param name="url">MongoUrl的String表示</param>
        /// <param name="collection">集合名称</param>
        /// <param name="query">查询条件</param>
        /// <returns>返回SafeModeResult</returns>
        public static SafeModeResult Remove(
            string url,
            string collection,
            IMongoQuery query)
        {
            return MongoHelper.Remove(url, collection, query, RemoveFlags.None);
        }

        /// <summary>
        /// 删除第一条匹配query的记录,如果query为null则删除集合的第一条记录
        /// </summary>
        /// <param name="url">MongoUrl的String表示</param>
        /// <param name="collection">集合名称</param>
        /// <param name="query">查询条件</param>
        /// <param name="flags">删除方式</param>
        /// <returns>返回SafeModeResult</returns>
        public static SafeModeResult Remove(
            string url,
            string collection,
            IMongoQuery query,
            RemoveFlags flags
            )
        {
            MongoCollection c = GetCollection(url, collection);
            return c.Remove(query, flags, SafeMode.True);
        }

        /// <summary>
        /// 返回由url和collection指定的MongoCollection对象
        /// </summary>
        /// <param name="url">MongoUrl的String表示</param>
        /// <param name="collection">集合名称</param>
        /// <returns></returns>
        private static MongoCollection GetCollection(
            string url,
            string collection)
        {
            MongoUrl u = new MongoUrl(url);
            MongoServer s = MongoServer.Create(u);
            MongoDatabase db = s.GetDatabase(u.DatabaseName);
            MongoCollection c = db.GetCollection(collection);

            return c;
        }
    }
}

  

转载于:https://www.cnblogs.com/YangFei-wow/archive/2012/09/20/2695402.html

 类似资料: