本文整理汇总了C#中MongoDatabase.RunCommand方法的典型用法代码示例。如果您正苦于以下问题:C# MongoDatabase.RunCommand方法的具体用法?C# MongoDatabase.RunCommand怎么用?C# MongoDatabase.RunCommand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoDatabase
的用法示例。
在下文中一共展示了MongoDatabase.RunCommand方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public bool Run(MongoDatabase db)
{
var findAndModifyDoc = Build();
var x = db.RunCommand(findAndModifyDoc, true);
if (x.Ok && x.HasResponse)
{
callOk = true;
responseDoc = (BsonDocument) x.Response["value"];
return true;
}
return false;
}
示例2: ExecuteMongoDBCommand
/// <summary>
/// 执行数据库命令
/// </summary>
/// <param name="mongoCmd"></param>
/// <param name="mongoDB"></param>
/// <returns></returns>
public static CommandResult ExecuteMongoDBCommand(String mongoCmd, MongoDatabase mongoDB)
{
CommandResult rtn;
try
{
rtn = mongoDB.RunCommand(mongoCmd);
}
catch (MongoCommandException ex)
{
rtn = ex.CommandResult;
}
return rtn;
}
示例3: Run
public AggregateResult Run(QupidQuery query, MongoDatabase db)
{
var aggResult = new AggregateResult();
string queryText = null;
try
{
queryText = query.GetMongoQuery();
}
catch (Exception e)
{
_errorManager.AddError(e.Message);
}
CommandDocument doc;
try
{
doc = new CommandDocument(BsonDocument.Parse(queryText));
}
catch (Exception e)
{
_errorManager.AddError("Error parsing document. " + e.Message);
return aggResult;
}
try
{
var result = db.RunCommand(doc);
if (!result.Ok)
{
_errorManager.AddError("Error running mongo command.");
return aggResult;
}
var resultArray = result.Response["result"].AsBsonArray;
ParseResult(resultArray, query, aggResult);
}
catch (MongoCommandException mce)
{
_errorManager.AddError("Error running mongo command. " + mce.Message);
return aggResult;
}
//if a group by - attempt to change the _id back to the group long name
//note: this needs to be run before plugins run because if a plugin references the groupby
//property it needs to be in full name form
if (query.GroupByClause != null && aggResult.Columns.Count > 0)
{
var idCol = aggResult.Columns.FirstOrDefault(c => c.ToLowerInvariant().Equals("_id"));
//the group by id column might already be converted by the runner if subobject
if (idCol != null)
{
var idColIdx = aggResult.Columns.IndexOf(idCol);
aggResult.Columns.RemoveAt(idColIdx);
aggResult.Columns.Insert(idColIdx, query.GroupByClause.AggregateByProperty.Path);
}
}
if (_joinPlugins != null)
{
foreach (var plugin in _joinPlugins)
{
//do column error checking
plugin.VerifySelectedColumns(_errorManager);
//then run the plugin
aggResult = plugin.RunJoin(aggResult);
}
}
//try to convert short names into long names (it will just skip plugin columns since it can't find them)
aggResult.Columns = aggResult.Columns.Select(c => query.Collection.ConvertToLongPath(c) ?? c).ToList();
return aggResult;
}
示例4: ExecuteMongoDBCommand
/// <summary>
/// 执行数据库命令
/// </summary>
/// <param name="mongoCmd"></param>
/// <param name="mongoDB"></param>
/// <returns></returns>
public static CommandResult ExecuteMongoDBCommand(CommandDocument mongoCmd, MongoDatabase mongoDB)
{
CommandResult mCommandResult;
try
{
mCommandResult = mongoDB.RunCommand(mongoCmd);
}
catch (MongoCommandException ex)
{
mCommandResult = ex.CommandResult;
}
var e = new RunCommandEventArgs
{
CommandString = mongoCmd.ToString(),
RunLevel = MongoDbHelper.PathLv.DatabaseLv,
Result = mCommandResult
};
OnCommandRunComplete(e);
return mCommandResult;
}
示例5: ExecuteMongoDBCommand
/// <summary>
/// 执行数据库命令
/// </summary>
/// <param name="mongoCmd"></param>
/// <param name="mongoDb"></param>
/// <returns></returns>
public static CommandResult ExecuteMongoDBCommand(CommandDocument mongoCmd, MongoDatabase mongoDb)
{
CommandResult mCommandResult;
try
{
mCommandResult = mongoDb.RunCommand(mongoCmd);
}
catch (MongoCommandException ex)
{
mCommandResult = new CommandResult(ex.Result);
}
var e = new RunCommandEventArgs
{
CommandString = mongoCmd.ToString(),
RunLevel = EnumMgr.PathLevel.Database,
Result = mCommandResult
};
OnCommandRunComplete(e);
return mCommandResult;
}
示例6: ShardCollection
static void ShardCollection(MongoCollection col, MongoDBAppender appender)
{
var cmd = new CommandDocument
{
{ "shardCollection", col.Database.Name + '.' + col.Name },
{ "key", new BsonDocument(appender.ShardKey, 1) },
};
try
{
CreateIndex(col, appender.ShardKey);
MongoServer server = col.Database.Server;
var dbCfg = new MongoDatabaseSettings();
var adminDb = new MongoDatabase(server, "admin", dbCfg);
server.Connect();
CommandResult shcr = adminDb.RunCommand(cmd);
}
catch (MongoCommandException mex)
{
if (!mex.Message.ToLower().Contains("already "))
throw;
else
Console.WriteLine("Sharding already enabled!");
}
}
示例7: EnableSharding
static void EnableSharding(MongoDatabase db)
{
var cmd = new CommandDocument("enablesharding", db.Name);
try
{
var dbCfg = new MongoDatabaseSettings();
var adminDb = new MongoDatabase(db.Server, "admin", dbCfg);
db.Server.Connect();
adminDb.RunCommand(cmd);
}
catch (MongoCommandException mex)
{
if (!mex.Message.ToLower().Contains("already enabled"))
throw;
else
Console.WriteLine("Sharding already enabled!");
}
}
示例8: StopDB
private void StopDB()
{
try
{
_('c', "Connecting to server process...");
var serverSettings = new MongoServerSettings();
serverSettings.Server = new MongoServerAddress("localhost");
var serverName = new MongoServer(serverSettings);
var databaseSettings = new MongoDatabaseSettings(serverName, "admin");
var db = new MongoDatabase(serverName, databaseSettings);
_('c', "Sending shutdown command...");
db.RunCommand(new CommandDocument { { "shutdown", 1 } });
}
catch{}
}
示例9: Run
/// <summary>
/// Executes the command and returned the output documents
/// </summary>
/// <param name="db">The db to execute the command on</param>
/// <returns></returns>
public List<BsonDocument> Run(MongoDatabase db)
{
var ret = db.RunCommand(Build(), true);
if (ret.Ok && ret.HasResponse)
{
callOk = true;
if (ret.Response.Has("results"))
{
var docs = (ret.Response["results"] as object[])
.OfType<BsonDocument>()
.ToList();
if (!string.IsNullOrEmpty(keyFieldName))
{
var key = "value." + keyFieldName;
foreach (var doc in docs)
{
doc[key] = doc["id"] = doc["_id"]; //id value is set to conform with the doc if read via select
}
return docs.Select(x => x["value"]).OfType<BsonDocument>().ToList();
}
//if no key is specified, return the entire document. not just the value
return docs;
}
else
{
var col = (string)ret.Response["result"];
var docs = db.GetCollection(col).Find().Select().OfType<BsonDocument>().ToList();
db.DropCollection(col);
if (!string.IsNullOrEmpty(keyFieldName))
{
var key = "value." + keyFieldName;
foreach (var doc in docs)
{
doc[key] = doc["id"];
}
return docs.Select(x=>x["value"]).OfType<BsonDocument>().ToList();
}
//if no key is specified, return the entire document, not just the value
return docs;
}
}
callOk = false;
//todo: log the error
return null;
}
示例10: ExecuteMongoDBCommand
/// <summary>
/// 执行数据库命令
/// </summary>
/// <param name="mongoCmd"></param>
/// <param name="mongoDB"></param>
/// <returns></returns>
public static CommandResult ExecuteMongoDBCommand(String mongoCmd, MongoDatabase mongoDB)
{
CommandResult mCommandResult;
try
{
mCommandResult = mongoDB.RunCommand(mongoCmd);
}
catch (MongoCommandException ex)
{
mCommandResult = ex.CommandResult;
}
RunCommandEventArgs e = new RunCommandEventArgs();
e.CommandString = mongoCmd;
e.RunLevel = PathLv.DatabaseLV;
e.Result = mCommandResult;
OnCommandRunComplete(e);
return mCommandResult;
}