/// <summary> /// 执行数据集命令 /// </summary> /// <param name="commandString"></param> /// <param name="mongoCol"></param> /// <returns></returns> public static CommandResult ExecuteMongoColCommand(string commandString, MongoCollection mongoCol) { CommandResult mCommandResult; var baseCommand = new BsonDocument { { commandString, mongoCol.Name } }; var mongoCmd = new CommandDocument(); mongoCmd.AddRange(baseCommand); try { mCommandResult = mongoCol.Database.RunCommand(mongoCmd); } catch (MongoCommandException ex) { mCommandResult = new CommandResult(ex.Result); } var e = new RunCommandEventArgs { CommandString = commandString, RunLevel = EnumMgr.PathLevel.CollectionAndView, Result = mCommandResult }; OnCommandRunComplete(e); return(mCommandResult); }
/// <summary> /// 执行数据集命令 /// </summary> /// <param name="CommandString"></param> /// <param name="mongoCol"></param> /// <returns></returns> public static CommandResult ExecuteMongoColCommand(String CommandString, MongoCollection mongoCol) { CommandResult mCommandResult; BsonDocument BaseCommand = new BsonDocument(); BaseCommand.Add(CommandString, mongoCol.Name); CommandDocument mongoCmd = new CommandDocument(); mongoCmd.AddRange(BaseCommand); try { mCommandResult = mongoCol.Database.RunCommand(mongoCmd); } catch (MongoCommandException ex) { mCommandResult = ex.CommandResult; } RunCommandEventArgs e = new RunCommandEventArgs(); e.CommandString = CommandString; e.RunLevel = PathLv.CollectionLV; e.Result = mCommandResult; OnCommandRunComplete(e); return(mCommandResult); }
/// <summary> /// 执行数据集命令 /// </summary> /// <param name="CommandString"></param> /// <param name="mongoCol"></param> /// <returns></returns> public static CommandResult ExecuteMongoColCommand(String CommandString, MongoCollection mongoCol) { CommandResult mCommandResult; var BaseCommand = new BsonDocument { { CommandString, mongoCol.Name } }; var mongoCmd = new CommandDocument(); mongoCmd.AddRange(BaseCommand); try { mCommandResult = mongoCol.Database.RunCommand(mongoCmd); } catch (MongoCommandException ex) { mCommandResult = ex.CommandResult; } var e = new RunCommandEventArgs { CommandString = CommandString, RunLevel = MongoDbHelper.PathLv.CollectionLv, Result = mCommandResult }; OnCommandRunComplete(e); return(mCommandResult); }
/// <summary> /// 初始化副本 /// </summary> /// <param name="mongoSvr">副本组主服务器</param> /// <param name="replicaSetName">副本名称</param> /// <param name="HostList">从属服务器列表</param> public static CommandResult InitReplicaSet(String replicaSetName, String HostList) { //第一台服务器作为Primary服务器 MongoClientSettings PrimarySetting = new MongoClientSettings(); PrimarySetting.Server = new MongoServerAddress(SystemManager.ConfigHelperInstance.ConnectionList[HostList].Host, SystemManager.ConfigHelperInstance.ConnectionList[HostList].Port); //如果不设置的话,会有错误:不是Primary服务器,SlaveOK 是 False PrimarySetting.ReadPreference = ReadPreference.PrimaryPreferred; MongoServer PrimarySvr = new MongoClient(PrimarySetting).GetServer(); BsonDocument config = new BsonDocument(); BsonArray hosts = new BsonArray(); BsonDocument replSetInitiateCmd = new BsonDocument(); BsonDocument host = new BsonDocument(); //生成命令 host = new BsonDocument(); host.Add(KEY_ID, 1); host.Add("host", SystemManager.ConfigHelperInstance.ConnectionList[HostList].Host + ":" + SystemManager.ConfigHelperInstance.ConnectionList[HostList].Port.ToString()); hosts.Add(host); config.Add(KEY_ID, replicaSetName); config.Add("members", hosts); replSetInitiateCmd.Add("replSetInitiate", config); CommandDocument mongoCmd = new CommandDocument(); mongoCmd.AddRange(replSetInitiateCmd); return(ExecuteMongoSvrCommand(mongoCmd, PrimarySvr)); }
/// 注意:有些命令可能只能用在mongos上面,例如addshard /// <summary> /// 使用Shell Helper命令 /// </summary> /// <param name="JsShell"></param> /// <param name="mongoSvr"></param> /// <returns></returns> public static CommandResult ExecuteJsShell(String JsShell, MongoServer mongoSvr) { var ShellCmd = new BsonDocument { { "$eval", new BsonJavaScript(JsShell) }, { "nolock", true } }; //必须nolock var mongoCmd = new CommandDocument(); mongoCmd.AddRange(ShellCmd); return(ExecuteMongoSvrCommand(mongoCmd, mongoSvr)); }
/// <summary> /// 初始化副本 /// </summary> /// <param name="replicaSetName">副本名称</param> /// <param name="hostList">从属服务器列表</param> /// <param name="configs"></param> public static CommandResult InitReplicaSet(string replicaSetName, string hostList, Dictionary <string, MongoConnectionConfig> configs) { //第一台服务器作为Primary服务器 var primarySetting = new MongoClientSettings { Server = new MongoServerAddress(configs[hostList].Host, configs[hostList].Port), ReadPreference = ReadPreference.PrimaryPreferred }; //如果不设置的话,会有错误:不是Primary服务器,SlaveOK 是 False var primarySvr = new MongoClient(primarySetting).GetServer(); var config = new BsonDocument(); var hosts = new BsonArray(); var replSetInitiateCmd = new BsonDocument(); var host = new BsonDocument(); //生成命令 host = new BsonDocument { { ConstMgr.KeyId, 1 }, { "host", configs[hostList].Host + ":" + configs[hostList].Port } }; hosts.Add(host); config.Add(ConstMgr.KeyId, replicaSetName); config.Add("members", hosts); replSetInitiateCmd.Add("replSetInitiate", config); var mongoCmd = new CommandDocument(); mongoCmd.AddRange(replSetInitiateCmd); return(ExecuteMongoSvrCommand(mongoCmd, primarySvr)); }
/// <summary> /// 初始化副本 /// </summary> /// <param name="replicaSetName">副本名称</param> /// <param name="hostList">从属服务器列表</param> public static CommandResult InitReplicaSet(String replicaSetName, String hostList) { //第一台服务器作为Primary服务器 var PrimarySetting = new MongoClientSettings { Server = new MongoServerAddress(SystemManager.ConfigHelperInstance.ConnectionList[hostList].Host, SystemManager.ConfigHelperInstance.ConnectionList[hostList].Port), ReadPreference = ReadPreference.PrimaryPreferred }; //如果不设置的话,会有错误:不是Primary服务器,SlaveOK 是 False MongoServer PrimarySvr = new MongoClient(PrimarySetting).GetServer(); var config = new BsonDocument(); var hosts = new BsonArray(); var replSetInitiateCmd = new BsonDocument(); var host = new BsonDocument(); //生成命令 host = new BsonDocument { { MongoDbHelper.KEY_ID, 1 }, { "host", SystemManager.ConfigHelperInstance.ConnectionList[hostList].Host + ":" + SystemManager.ConfigHelperInstance.ConnectionList[hostList].Port } }; hosts.Add(host); config.Add(MongoDbHelper.KEY_ID, replicaSetName); config.Add("members", hosts); replSetInitiateCmd.Add("replSetInitiate", config); var mongoCmd = new CommandDocument(); mongoCmd.AddRange(replSetInitiateCmd); return(ExecuteMongoSvrCommand(mongoCmd, PrimarySvr)); }