Example #1
0
 /// <summary>
 ///     执行聚合
 /// </summary>
 /// <param name="aggregateDoc"></param>
 /// <returns></returns>
 /// <param name="collectionName"></param>
 public static CommandResult Aggregate(BsonArray aggregateDoc, string collectionName)
 {
     //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
     var aggrCmd = new CommandDocument
         {
             new BsonElement("aggregate", new BsonString(collectionName)),
             new BsonElement("pipeline", aggregateDoc)
         };
     var aggregateCommand = new MongoCommand(aggrCmd, EnumMgr.PathLevel.Database);
     return ExecuteMongoCommand(aggregateCommand);
 }
Example #2
0
 /// <summary>
 ///     执行Count(指定路劲)
 /// </summary>
 /// <param name="QueryDoc"></param>
 /// <param name="databaseName"></param>
 /// <param name="collectionName"></param>
 /// <returns></returns>
 public static CommandResult Count(BsonDocument QueryDoc, string databaseName, string collectionName)
 {
     //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
     var aggrCmd = new CommandDocument
         {
             new BsonElement("count", new BsonString(collectionName)),
             new BsonElement("query", QueryDoc)
         };
     var aggregateCommand = new MongoCommand(aggrCmd, EnumMgr.PathLevel.Database, databaseName);
     return ExecuteMongoCommand(aggregateCommand);
 }
        /// <summary>
        ///     当前对象的MONGO命令
        /// </summary>
        /// <param name="mMongoCommand">命令对象</param>
        /// <param name="ShowMsgBox"></param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoCommand(MongoCommand mMongoCommand, Boolean ShowMsgBox = true)
        {
            var ResultCommandList = new List<CommandResult>();

            var mCommandResult = new CommandResult(new BsonDocument());
            try
            {
                switch (mMongoCommand.RunLevel)
                {
                    case MongoDbHelper.PathLv.CollectionLv:
                        if (String.IsNullOrEmpty(mMongoCommand.CommandString))
                        {
                            mCommandResult = ExecuteMongoColCommand(mMongoCommand.cmdDocument,
                                SystemManager.GetCurrentCollection());
                        }
                        else
                        {
                            mCommandResult = ExecuteMongoColCommand(mMongoCommand.CommandString,
                                SystemManager.GetCurrentCollection());
                        }
                        break;
                    case MongoDbHelper.PathLv.DatabaseLv:
                        mCommandResult = ExecuteMongoDBCommand(mMongoCommand.cmdDocument,
                            SystemManager.GetCurrentDataBase());
                        break;
                    case MongoDbHelper.PathLv.InstanceLv:
                        mCommandResult = ExecuteMongoSvrCommand(mMongoCommand.cmdDocument,
                            SystemManager.GetCurrentServer());
                        break;
                    default:
                        break;
                }
                ResultCommandList.Add(mCommandResult);
                if (ShowMsgBox)
                    MyMessageBox.ShowMessage(mMongoCommand.CommandString, mMongoCommand.CommandString + " Result",
                        MongoDbHelper.ConvertCommandResultlstToString(ResultCommandList), true);
            }
            catch (IOException ex)
            {
                SystemManager.ExceptionDeal(ex, mMongoCommand.CommandString,
                    "IOException,Try to set Socket TimeOut more long at connection config");
            }
            catch (Exception ex)
            {
                SystemManager.ExceptionDeal(ex, mMongoCommand.CommandString);
            }

            return mCommandResult;
        }
 public static CommandResult Aggregate(BsonArray AggregateDoc)
 {
     //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
     try
     {
         CommandDocument agg = new CommandDocument();
         agg.Add(new BsonElement("aggregate", new BsonString(SystemManager.GetCurrentCollection().Name)));
         agg.Add(new BsonElement("pipeline", AggregateDoc));
         MongoCommand Aggregate_Command = new MongoCommand(agg, PathLv.DatabaseLV);
         return ExecuteMongoCommand(Aggregate_Command, false);
     }
     catch (Exception ex)
     {
         SystemManager.ExceptionDeal(ex);
         return new CommandResult(new BsonDocument());
     }
 }
Example #5
0
        /// <summary>
        ///     当前对象的MONGO命令
        /// </summary>
        /// <param name="mMongoCommand">命令对象</param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoCommand(MongoCommand mMongoCommand)
        {
            var resultCommandList = new List<CommandResult>();

            var mCommandResult = new CommandResult(new BsonDocument());
            try
            {
                switch (mMongoCommand.RunLevel)
                {
                    case EnumMgr.PathLevel.Collection:
                        if (string.IsNullOrEmpty(mMongoCommand.CommandString))
                        {
                            mCommandResult = ExecuteMongoColCommand(mMongoCommand.CmdDocument,
                                RuntimeMongoDbContext.GetCurrentCollection());
                        }
                        else
                        {
                            mCommandResult = ExecuteMongoColCommand(mMongoCommand.CommandString,
                                RuntimeMongoDbContext.GetCurrentCollection());
                        }
                        break;
                    case EnumMgr.PathLevel.Database:
                        mCommandResult = ExecuteMongoDBCommand(mMongoCommand.CmdDocument,
                            RuntimeMongoDbContext.GetCurrentDataBase());
                        break;
                    case EnumMgr.PathLevel.Instance:
                        mCommandResult = ExecuteMongoSvrCommand(mMongoCommand.CmdDocument,
                            RuntimeMongoDbContext.GetCurrentServer());
                        break;
                }
                resultCommandList.Add(mCommandResult);
            }
            catch (IOException ex)
            {
                Utility.ExceptionDeal(ex, mMongoCommand.CommandString,
                    "IOException,Try to set Socket TimeOut more long at connection config");
            }
            catch (Exception ex)
            {
                Utility.ExceptionDeal(ex, mMongoCommand.CommandString);
            }

            return mCommandResult;
        }
 /// <summary>
 /// 在指定服务器上执行指定命令
 /// </summary>
 /// <param name="mMongoCommand"></param>
 /// <param name="mongosrv"></param>
 /// <returns></returns>
 public static CommandResult ExecuteMongoSvrCommand(MongoCommand mMongoCommand, MongoServer mongosrv)
 {
     var Command = new CommandDocument { { mMongoCommand.CommandString, 1 } };
     if (mMongoCommand.RunLevel == PathLv.DatabaseLV)
     {
         throw new Exception();
     }
     else
     {
         return ExecuteMongoSvrCommand(Command, mongosrv);
     }
 }
 /// <summary>
 ///     在指定数据库执行指定命令
 /// </summary>
 /// <param name="mMongoCommand"></param>
 /// <param name="mongoDB"></param>
 /// <returns></returns>
 public static CommandResult ExecuteMongoDBCommand(MongoCommand mMongoCommand, MongoDatabase mongoDB)
 {
     var Command = new CommandDocument {{mMongoCommand.CommandString, 1}};
     if (mMongoCommand.RunLevel == MongoDbHelper.PathLv.DatabaseLv)
     {
         return ExecuteMongoDBCommand(Command, mongoDB);
     }
     throw new Exception();
 }
Example #8
0
 /// <summary>
 ///     执行聚合
 /// </summary>
 /// <param name="aggregateDoc"></param>
 /// <returns></returns>
 /// <param name="collectionName"></param>
 public static CommandResult Aggregate(BsonArray aggregateDoc, string collectionName)
 {
     //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
     try
     {
         var agg = new CommandDocument
         {
             new BsonElement("aggregate", new BsonString(collectionName)),
             new BsonElement("pipeline", aggregateDoc)
         };
         var aggregateCommand = new MongoCommand(agg, EnumMgr.PathLevel.Database);
         return ExecuteMongoCommand(aggregateCommand);
     }
     catch (Exception ex)
     {
         Utility.ExceptionDeal(ex);
         return new CommandResult(new BsonDocument());
     }
 }
Example #9
0
 /// <summary>
 ///     在指定服务器上执行指定命令
 /// </summary>
 /// <param name="mMongoCommand"></param>
 /// <param name="mongosrv"></param>
 /// <returns></returns>
 public static CommandResult ExecuteMongoSvrCommand(MongoCommand mMongoCommand, MongoServer mongosrv)
 {
     var command = new CommandDocument {{mMongoCommand.CommandString, 1}};
     if (mMongoCommand.RunLevel == EnumMgr.PathLevel.Database)
     {
         throw new Exception();
     }
     return ExecuteMongoSvrCommand(command, mongosrv);
 }
 /// <summary>
 /// 在指定数据库执行指定命令
 /// </summary>
 /// <param name="cmd"></param>
 /// <param name="mongoDB"></param>
 /// <returns></returns>
 public static CommandResult RunMongoCommandAtMongoDB(MongoCommand cmd, MongoDatabase mongoDB)
 {
     var Command = new CommandDocument { { cmd.CommandString, 1 } };
     if (cmd.RunLevel == PathLv.DatabaseLV)
     {
         return ExecuteMongoDBCommand(Command, mongoDB);
     }
     else
     {
         throw new Exception();
     }
 }
 /// <summary>
 /// 当前对象的MONGO命令
 /// </summary>
 /// <param name="cmd">命令对象</param>
 /// <returns></returns>
 public static CommandResult RunMongoCommandAtCurrentObj(MongoCommand cmd)
 {
     var Command = new CommandDocument { { cmd.CommandString, 1 } };
     CommandResult rtn = new CommandResult();
     switch (cmd.RunLevel)
     {
         case PathLv.CollectionLV:
             rtn = ExecuteMongoColCommand(cmd.CommandString, SystemManager.GetCurrentCollection());
             break;
         case PathLv.DatabaseLV:
             rtn =  ExecuteMongoDBCommand(Command, SystemManager.GetCurrentDataBase());
             break;
         case PathLv.ServerLV:
             rtn =  ExecuteMongoSvrCommand(Command, SystemManager.GetCurrentService());
             break;
         default:
             break;
     }
     return rtn;
 }
Example #12
0
        /// <summary>
        ///     当前对象的MONGO命令
        /// </summary>
        /// <param name="mMongoCommand">命令对象</param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoCommand(MongoCommand mMongoCommand)
        {
            var resultCommandList = new List<CommandResult>();

            var mCommandResult = new CommandResult(new BsonDocument());
            switch (mMongoCommand.RunLevel)
            {
                case EnumMgr.PathLevel.CollectionAndView:
                    if (string.IsNullOrEmpty(mMongoCommand.CommandString))
                    {
                        mCommandResult = ExecuteMongoColCommand(mMongoCommand.CmdDocument,
                            RuntimeMongoDbContext.GetCurrentCollection());
                    }
                    else
                    {
                        mCommandResult = ExecuteMongoColCommand(mMongoCommand.CommandString,
                            RuntimeMongoDbContext.GetCurrentCollection());
                    }
                    break;
                case EnumMgr.PathLevel.Database:
                    if (string.IsNullOrEmpty(mMongoCommand.DatabaseName))
                    {
                        mCommandResult = ExecuteMongoDBCommand(mMongoCommand.CmdDocument, RuntimeMongoDbContext.GetCurrentDataBase());
                    }
                    else
                    {
                        var db = RuntimeMongoDbContext.GetCurrentClient().GetDatabase(mMongoCommand.DatabaseName);
                        mCommandResult = ExecuteMongoDBCommand(mMongoCommand.CmdDocument, db);
                    }
                    break;
                case EnumMgr.PathLevel.Instance:
                    mCommandResult = ExecuteMongoSvrCommand(mMongoCommand.CmdDocument,
                        RuntimeMongoDbContext.GetCurrentServer());
                    break;
            }
            resultCommandList.Add(mCommandResult);
            return mCommandResult;
        }
        /// <summary>
        /// 当前对象的MONGO命令
        /// </summary>
        /// <param name="cmd">命令对象</param>
        /// <returns></returns>
        public static void ExecuteMongoCommand(MongoCommand cmd)
        {
            var Command = new CommandDocument { { cmd.CommandString, 1 } };
            List<CommandResult> ResultCommandList = new List<CommandResult>();

            CommandResult rtn = new CommandResult();
            try
            {
                switch (cmd.RunLevel)
                {
                    case PathLv.CollectionLV:
                        rtn = ExecuteMongoColCommand(cmd.CommandString, SystemManager.GetCurrentCollection());
                        break;
                    case PathLv.DatabaseLV:
                        rtn = ExecuteMongoDBCommand(Command, SystemManager.GetCurrentDataBase());
                        break;
                    case PathLv.ServerLV:
                        rtn = ExecuteMongoSvrCommand(Command, SystemManager.GetCurrentServer());
                        break;
                    default:
                        break;
                }
                ResultCommandList.Add(rtn);
                MyMessageBox.ShowMessage(cmd.CommandString, cmd.CommandString + " Result", MongoDBHelper.ConvertCommandResultlstToString(ResultCommandList), true);
            }
            catch (System.IO.IOException ex)
            {
                MyMessageBox.ShowMessage(cmd.CommandString, "IOException,Try to set Socket TimeOut more long at connection config", ex.ToString(), true);
            }
            catch (Exception ex)
            {
                SystemManager.ExceptionDeal(ex, cmd.CommandString);
            }
        }