/// <summary>
        ///     增加数据分片
        /// </summary>
        /// <param name="routeSvr"></param>
        /// <param name="replicaSetName"></param>
        /// <param name="lstAddress"></param>
        /// <remarks>注意:有个命令可能只能用在mongos上面</remarks>
        /// <returns></returns>
        public static CommandResult AddSharding(MongoServer routeSvr, String replicaSetName, List <String> lstAddress,
                                                String Name, Decimal MaxSize)
        {
            // replset/host:port,host:port
            String cmdPara = replicaSetName == String.Empty ? String.Empty : (replicaSetName + "/");

            foreach (String item in lstAddress)
            {
                cmdPara += item + ",";
            }
            cmdPara = cmdPara.TrimEnd(",".ToCharArray());
            var mongoCmd = new CommandDocument {
                { "addshard", cmdPara }
            };

            if (MaxSize != 0)
            {
                mongoCmd.Add("maxSize", (BsonValue)MaxSize);
            }
            if (Name != String.Empty)
            {
                mongoCmd.Add("name", Name);
            }

            return(ExecuteMongoSvrCommand(mongoCmd, routeSvr));
        }
예제 #2
0
        /// <summary>
        ///     增加数据分片
        /// </summary>
        /// <param name="routeSvr"></param>
        /// <param name="replicaSetName"></param>
        /// <param name="lstAddress"></param>
        /// <remarks>注意:有个命令可能只能用在mongos上面</remarks>
        /// <returns></returns>
        public static CommandResult AddSharding(MongoServer routeSvr, string replicaSetName, List <string> lstAddress,
                                                string name, decimal maxSize)
        {
            // replset/host:port,host:port
            var cmdPara = replicaSetName == string.Empty ? string.Empty : replicaSetName + "/";

            foreach (var item in lstAddress)
            {
                cmdPara += item + ",";
            }
            cmdPara = cmdPara.TrimEnd(",".ToCharArray());
            var mongoCmd = new CommandDocument {
                { "addshard", cmdPara }
            };

            if (maxSize != 0)
            {
                mongoCmd.Add("maxSize", (BsonValue)maxSize);
            }
            if (name != string.Empty)
            {
                mongoCmd.Add("name", name);
            }

            return(ExecuteMongoSvrCommand(mongoCmd, routeSvr));
        }
예제 #3
0
        /// <summary>
        /// 数据集分片
        /// </summary>
        /// <param name="routeSvr"></param>
        /// <param name="sharingCollection"></param>
        /// <param name="shardingKey"></param>
        /// <returns></returns>
        public static CommandResult ShardCollection(MongoServer routeSvr, String sharingCollection, BsonDocument shardingKey)
        {
            CommandDocument mongoCmd = new CommandDocument();

            mongoCmd.Add("shardCollection", sharingCollection);
            mongoCmd.Add("key", shardingKey);
            return(ExecuteMongoSvrCommand(mongoCmd, routeSvr));
        }
예제 #4
0
        /// <summary>
        ///     AddTagRange
        /// </summary>
        /// <param name="routeSvr">路由服务器</param>
        /// <param name="nameSpace">名字空间</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <param name="tag">标签</param>
        /// <returns></returns>
        public static CommandResult updateZoneKeyRange(MongoServer routeSvr, string nameSpace, string FieldName, BsonValue min, BsonValue max, string zone)
        {
            var mongoCmd = new CommandDocument {
                { "updateZoneKeyRange", nameSpace }
            };

            mongoCmd.Add("min", new BsonDocument(FieldName, min));
            mongoCmd.Add("max", new BsonDocument(FieldName, max));
            mongoCmd.Add("zone", zone);
            return(ExecuteMongoSvrCommand(mongoCmd, routeSvr));
        }
예제 #5
0
        /// <summary>
        ///     添加自定义角色
        /// </summary>
        public static CommandResult createRole(MongoDatabase mongoDb, Role role)
        {
            var mongoCmd   = new CommandDocument("createRole", role.Rolename);
            var privileges = new BsonArray();

            privileges.AddRange(role.Privileges.Select(x => x.AsBsonDocument()));
            mongoCmd.Add("privileges", privileges);
            var roles = new BsonArray();

            roles.AddRange(role.Roles.Select(x => x.AsBsonValue()));
            mongoCmd.Add("roles", roles);
            return(ExecuteMongoDBCommand(mongoCmd, mongoDb));
        }
예제 #6
0
        /// <summary>
        /// 移除Shard
        /// </summary>
        /// <param name="routeSvr"></param>
        /// <param name="ShardName">Shard名称</param>
        /// <returns></returns>
        public static CommandResult RemoveSharding(MongoServer routeSvr, String ShardName)
        {
            CommandDocument mongoCmd = new CommandDocument();

            mongoCmd.Add("removeshard", ShardName);
            return(ExecuteMongoSvrCommand(mongoCmd, routeSvr));
        }
예제 #7
0
        /// <summary>
        ///     修改用户(完全替换)
        /// </summary>
        /// <param name="user"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static CommandResult updateUser(MongoUserEx user, MongoDatabase db)
        {
            var mongoCmd = new CommandDocument {
                { "updateUser", user.Username }
            };

            mongoCmd.Add("pwd", user.Password);
            var roles = new BsonArray();

            roles.AddRange(user.Roles.Select(x => x.AsBsonValue()));
            mongoCmd.Add("roles", roles);
            if (user.customData != null)
            {
                mongoCmd.Add("customData", user.customData);
            }
            return(ExecuteMongoDBCommand(mongoCmd, db));
        }
예제 #8
0
        /// <summary>
        /// 数据库分片
        /// </summary>
        /// <param name="routeSvr"></param>
        /// <param name="shardingDB"></param>
        /// <returns></returns>
        public static CommandResult EnableSharding(MongoServer routeSvr, String shardingDB)
        {
            CommandDocument mongoCmd = new CommandDocument();

            mongoCmd = new CommandDocument();
            mongoCmd.Add("enablesharding", shardingDB);
            return(ExecuteMongoSvrCommand(mongoCmd, routeSvr));
        }
 /// <summary>
 /// 执行聚合
 /// </summary>
 /// <param name="AggregateDoc"></param>
 /// <returns></returns>
 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()));
     }
 }
예제 #10
0
        /// <summary>
        ///     convertToCapped
        /// </summary>
        /// <param name="collectionName"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static CommandResult convertToCapped(string collectionName, long size, MongoDatabase db)
        {
            var mongoCmd = new CommandDocument {
                { "convertToCapped", collectionName }
            };

            mongoCmd.Add("size", size);
            return(ExecuteMongoDBCommand(mongoCmd, db));
        }
예제 #11
0
        /// <summary>
        ///     AddShardToZone
        /// </summary>
        /// <param name="routeSvr">服务器</param>
        /// <param name="shardName">Shard名称</param>
        /// <param name="tagName">Tag名称</param>
        /// <returns></returns>
        public static CommandResult AddShardToZone(MongoServer routeSvr, string shardName, string zone)
        {
            var mongoCmd = new CommandDocument {
                { "addShardToZone", shardName }
            };

            mongoCmd.Add("zone", zone);
            return(ExecuteMongoSvrCommand(mongoCmd, routeSvr));
        }
예제 #12
0
        /// <summary>
        ///     重新启动
        /// </summary>
        /// <param name="primarySvr">副本组主服务器</param>
        /// <param name="config">服务器信息</param>
        /// <remarks>这个命令C#无法正确执行</remarks>
        /// <returns></returns>
        public static CommandResult ReconfigReplsetServer(MongoServer primarySvr, BsonDocument config, Boolean force = false)
        {
            var mongoCmd = new CommandDocument {
                { "replSetReconfig", config }
            };

            mongoCmd.Add("force", force);
            return(ExecuteMongoSvrCommand(mongoCmd, primarySvr));
        }
예제 #13
0
        public static void turnNoTableScanOnOff(bool status)
        {
            MongoClient mc = new MongoClient("mongodb://aks-osx-centos:23021/");

            MongoServer     ms  = mc.GetServer();
            MongoDatabase   mDb = ms.GetDatabase("admin");
            CommandDocument cd  = new CommandDocument();

            cd.Add("setParameter", new BsonBoolean(true));
            cd.Add("notablescan", new BsonBoolean(status));
            using (ms.RequestStart(mDb)) {
                try {
                    CommandResult cr = mDb.RunCommand(cd);
                    Console.WriteLine("Changed [notablescan = " + status + "] : " + cr.ToString());
                } catch (Exception ex) {
                    Console.WriteLine("Caught Exception within While Loop: " + ex.StackTrace);
                }
            }
        }
예제 #14
0
        /// <summary>
        ///     Creates the begin transaction command document.
        /// </summary>
        /// <returns></returns>
        private CommandDocument CreateBeginTransactionCommandDocument()
        {
            var commandDocument = new CommandDocument {
                new BsonElement("beginTransaction", "1")
            };

            switch (Options.Isolation)
            {
            default:
                //case TransactionIsolation.Mvcc:
                commandDocument.Add(new BsonElement("isolation", "mvcc"));
                break;

            case MongoTransactionIsolation.ReadUncommitted:
                commandDocument.Add(new BsonElement("isolation", "readUncommitted"));
                break;

            case MongoTransactionIsolation.Serializable:
                commandDocument.Add(new BsonElement("isolation", "serializable"));
                break;
            }

            return(commandDocument);
        }
        /// <summary>
        /// 数据集命令
        /// </summary>
        /// <param name="Command">命令关键字</param>
        /// <param name="mongoCol">数据集</param>
        /// <param name="ExtendInfo">命令参数</param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoColCommand(String Command, MongoCollection mongoCol, BsonDocument ExtendInfo)
        {
            var textSearchCommand = new CommandDocument
            {
                { Command, mongoCol.Name },
            };

            foreach (var item in ExtendInfo.Elements)
            {
                textSearchCommand.Add(item);
            }
            var mCommandResult    = mongoCol.Database.RunCommand(textSearchCommand);
            RunCommandEventArgs e = new RunCommandEventArgs();

            e.CommandString = textSearchCommand.ToString();
            e.RunLevel      = PathLv.CollectionLV;
            e.Result        = mCommandResult;
            OnCommandRunComplete(e);
            return(mCommandResult);
        }
예제 #16
0
        /// <summary>
        ///     数据集命令
        /// </summary>
        /// <param name="command">命令关键字</param>
        /// <param name="mongoCol">数据集</param>
        /// <param name="extendInfo">命令参数</param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoColCommand(string command, MongoCollection mongoCol,
                                                           BsonDocument extendInfo)
        {
            var executeCommand = new CommandDocument
            {
                { command, mongoCol.Name }
            };

            foreach (var item in extendInfo.Elements)
            {
                executeCommand.Add(item);
            }
            var mCommandResult = mongoCol.Database.RunCommand(executeCommand);
            var e = new RunCommandEventArgs
            {
                CommandString = executeCommand.ToString(),
                RunLevel      = EnumMgr.PathLevel.CollectionAndView,
                Result        = mCommandResult
            };

            OnCommandRunComplete(e);
            return(mCommandResult);
        }
        /// <summary>
        ///     数据集命令
        /// </summary>
        /// <param name="Command">命令关键字</param>
        /// <param name="mongoCol">数据集</param>
        /// <param name="ExtendInfo">命令参数</param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoColCommand(String Command, MongoCollection mongoCol,
                                                           BsonDocument ExtendInfo)
        {
            var ExecuteCommand = new CommandDocument
            {
                { Command, mongoCol.Name },
            };

            foreach (BsonElement item in ExtendInfo.Elements)
            {
                ExecuteCommand.Add(item);
            }
            CommandResult mCommandResult = mongoCol.Database.RunCommand(ExecuteCommand);
            var           e = new RunCommandEventArgs
            {
                CommandString = ExecuteCommand.ToString(),
                RunLevel      = MongoDbHelper.PathLv.CollectionLv,
                Result        = mCommandResult
            };

            OnCommandRunComplete(e);
            return(mCommandResult);
        }
예제 #18
0
        public IList <T> Execute <T>(string command, ParamList parameters) where T : class
        {
            IList <T> items = new T[] { };
            var       args  = parameters ?? new Dictionary <string, object>();

            switch (command)
            {
            case "aggregate":
            {
                object collection, pipeline;
                if ((args.TryGetValue("collection", out collection) && collection is string) &&
                    (args.TryGetValue("pipeline", out pipeline) && pipeline is BsonArray))
                {
                    var result = _context.Session.RunCommand(new CommandDocument
                        {
                            { "aggregate", (string)collection },
                            { "pipeline", (BsonArray)pipeline }
                        });
                    items =
                        result.Response["result"].AsBsonArray.Select(
                            v => BsonSerializer.Deserialize <T>(v.AsBsonDocument)).ToArray();
                }
            }
            break;

            case "mapReduce":
            {
                object collection, map, reduce;
                if ((args.TryGetValue("collection", out collection) && collection is string) &&
                    (args.TryGetValue("map", out map) && (map is string || map is BsonJavaScript)) &&
                    (args.TryGetValue("reduce", out reduce) && (reduce is string || reduce is BsonJavaScript)))
                {
                    var commandDoc = new CommandDocument {
                        { "mapReduce", (string)collection }
                    };

                    var s1 = map as string;
                    if (s1 != null)
                    {
                        commandDoc.Add("map", new BsonJavaScript(s1));
                    }
                    else
                    {
                        commandDoc.Add("map", (BsonJavaScript)map);
                    }

                    var s2 = reduce as string;
                    if (s2 != null)
                    {
                        commandDoc.Add("reduce", new BsonJavaScript(s2));
                    }
                    else
                    {
                        commandDoc.Add("reduce", (BsonJavaScript)reduce);
                    }

                    object query;
                    if (args.TryGetValue("query", out query) && (query is string || query is BsonDocument))
                    {
                        var s = query as string;
                        if (s != null)
                        {
                            commandDoc.Add("query", BsonDocument.Parse(s));
                        }
                        else
                        {
                            commandDoc.Add("query", (BsonDocument)query);
                        }
                    }

                    object sort;
                    if (args.TryGetValue("sort", out sort) && (sort is string || sort is BsonDocument))
                    {
                        var s = sort as string;
                        if (s != null)
                        {
                            commandDoc.Add("sort", BsonDocument.Parse(s));
                        }
                        else
                        {
                            commandDoc.Add("sort", (BsonDocument)sort);
                        }
                    }

                    object limit;
                    if (args.TryGetValue("limit", out limit) && limit is int)
                    {
                        commandDoc.Add("limit", (int)limit);
                    }

                    object finalize;
                    if (args.TryGetValue("finalize", out finalize) &&
                        (finalize is string || finalize is BsonJavaScript))
                    {
                        var s = finalize as string;
                        if (s != null)
                        {
                            commandDoc.Add("finalize", BsonDocument.Parse(s));
                        }
                        else
                        {
                            commandDoc.Add("finalize", (BsonDocument)finalize);
                        }
                    }

                    var result = _context.Session.RunCommand(commandDoc);
                    items = result.Response["result"].AsBsonArray.Select(v => BsonSerializer.Deserialize <T>(v.AsBsonDocument)).ToArray();
                }
            }
            break;
            }
            return(items);
        }
예제 #19
0
        private static void CreateIndexes(MongoCollection <BsonDocument> sourceCollection, MongoCollection <BsonDocument> targetCollection, FlexibleOptions options)
        {
            if (options == null)
            {
                options = new FlexibleOptions();
            }
            var logger = NLog.LogManager.GetLogger("CreateIndexes");

            logger.Debug("{2} - {0}.{1} - Start index creation", sourceCollection.Database.Name, sourceCollection.Name, Thread.CurrentThread.ManagedThreadId);

            var command = new CommandDocument();

            command.Add("createIndexes", targetCollection.Name);
            var indexList = new BsonArray();

            command.Add("indexes", indexList);

            // Copying Indexes - If Any
            foreach (IndexInfo idx in sourceCollection.GetIndexes().ToList())
            {
                // Skipping "_id_" default index - Since Every mongodb Collection has it
                if (idx.Name == "_id_")
                {
                    continue;
                }

                // Recreating Index Options based on the current index options
                var opts = IndexOptions.SetBackground(idx.IsBackground || options.Get("indexes-background", false))
                           .SetSparse(idx.IsSparse || options.Get("indexes-sparse", false))
                           .SetUnique(idx.IsUnique).SetName(idx.Name).SetDropDups(idx.DroppedDups);

                if (idx.TimeToLive < TimeSpan.MaxValue)
                {
                    opts.SetTimeToLive(idx.TimeToLive);
                }

                // Adding Index
                try
                {
                    if (targetCollection.Database.Server.BuildInfo.Version.Major < 2 && targetCollection.Database.Server.BuildInfo.Version.MajorRevision < 6)
                    {
                        logger.Debug("{2} - {0}.{1} - Creating index: {2}", sourceCollection.Database.Name, sourceCollection, idx.Name, Thread.CurrentThread.ManagedThreadId);
                        targetCollection.CreateIndex(idx.Key, opts);
                    }
                    else
                    {
                        logger.Debug("{2} - {0}.{1} - Prepare index creation: {2}", sourceCollection.Database.Name, sourceCollection, idx.Name, Thread.CurrentThread.ManagedThreadId);
                        // removes the namespace to allow mongodb to generate the correct one...
                        var doc = idx.RawDocument;
                        doc.Remove("ns");
                        if (options.Get("indexes-background", false))
                        {
                            doc["background"] = true;
                        }
                        if (options.Get("indexes-sparse", false))
                        {
                            doc["sparse"] = true;
                        }
                        indexList.Add(doc);
                    }
                }
                catch (Exception ex)
                {
                    // check for timeout exception that may occur if the collection is large...
                    if (ex is System.IO.IOException || ex is System.Net.Sockets.SocketException || (ex.InnerException != null && ex.InnerException is System.Net.Sockets.SocketException))
                    {
                        logger.Warn("{3} - {0}.{1} - Timeout creating index {2}, this may occur in large collections. You should check manually after a while.", sourceCollection.Database.Name, sourceCollection.Name, idx.Name, Thread.CurrentThread.ManagedThreadId);
                        // wait for index creation....
                        for (var i = 0; i < 30; i++)
                        {
                            System.Threading.Thread.Sleep(10000);
                            try
                            {
                                if (targetCollection.IndexExists(idx.Name))
                                {
                                    break;
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    else
                    {
                        logger.Error(ex, "{0}.{1} - Error creating index {2}" + idx.Name);
                    }

                    logger.Warn("{3} - {0}.{1} - Index details: {2}", sourceCollection.Database.Name, sourceCollection.Name, idx.RawDocument.ToJson(), Thread.CurrentThread.ManagedThreadId);
                }
            }

            if (indexList.Count > 0)
            {
                try
                {
                    logger.Debug("{3} - {0}.{1} - Creating {2} indexes", sourceCollection.Database.Name, sourceCollection, indexList.Count, Thread.CurrentThread.ManagedThreadId);
                    targetCollection.Database.RunCommand(command);
                }
                catch (Exception ex)
                {
                    // check for timeout exception that may occur if the collection is large...
                    if (ex is System.IO.IOException || ex is System.Net.Sockets.SocketException || (ex.InnerException != null && ex.InnerException is System.Net.Sockets.SocketException))
                    {
                        logger.Warn("{3} - {0}.{1} - Timeout creating {2} indexes, this may occur in large collections. You should check manually after a while.", sourceCollection.Database.Name, sourceCollection.Name, indexList.Count, Thread.CurrentThread.ManagedThreadId);
                        logger.Warn("{3} - {0}.{1} - Index details: {2}", sourceCollection.Database.Name, sourceCollection.Name, command.ToJson(), Thread.CurrentThread.ManagedThreadId);
                    }
                    else
                    {
                        logger.Error(ex, "{2} - {0}.{1} - Error creating indexes", sourceCollection.Database.Name, sourceCollection.Name, Thread.CurrentThread.ManagedThreadId);
                        logger.Error("{3} - {0}.{1} - Index details: {2}", sourceCollection.Database.Name, sourceCollection.Name, command.ToJson(), Thread.CurrentThread.ManagedThreadId);
                    }
                }
            }

            logger.Debug("{2} - {0}.{1} - Index creation completed", sourceCollection.Database.Name, sourceCollection, Thread.CurrentThread.ManagedThreadId);
        }