// this is a low level method that doesn't require a MongoServer // so it can be used while connecting to a MongoServer internal CommandResult RunCommand( string collectionName, QueryFlags queryFlags, CommandDocument command ) { var commandName = command.GetElement(0).Name; var writerSettings = new BsonBinaryWriterSettings { GuidRepresentation = GuidRepresentation.Unspecified, MaxDocumentSize = serverInstance.MaxDocumentSize }; using ( var message = new MongoQueryMessage( writerSettings, collectionName, queryFlags, 0, // numberToSkip 1, // numberToReturn (must be 1 or -1 for commands) command, null // fields ) ) { SendMessage(message, SafeMode.False); } var readerSettings = new BsonBinaryReaderSettings { GuidRepresentation = GuidRepresentation.Unspecified, MaxDocumentSize = serverInstance.MaxDocumentSize }; var reply = ReceiveMessage <BsonDocument>(readerSettings, null); if (reply.NumberReturned == 0) { var message = string.Format("Command '{0}' failed. No response returned.", commandName); throw new MongoCommandException(message); } var commandResult = new CommandResult(command, reply.Documents[0]); if (!commandResult.Ok) { throw new MongoCommandException(commandResult); } return(commandResult); }
/// <summary> /// 执行数据集命令 /// </summary> /// <param name="CmdDoc"></param> /// <param name="mongoCol"></param> /// <returns></returns> public static CommandResult ExecuteMongoColCommand(CommandDocument CmdDoc, MongoCollection mongoCol) { CommandResult mCommandResult; try { mCommandResult = mongoCol.Database.RunCommand(CmdDoc); } catch (MongoCommandException ex) { mCommandResult = ex.CommandResult; } RunCommandEventArgs e = new RunCommandEventArgs(); e.CommandString = CmdDoc.GetElement(0).Value.ToString(); e.RunLevel = PathLv.DatabaseLV; e.Result = mCommandResult; OnCommandRunComplete(e); return(mCommandResult); }
// this is a low level method that doesn't require a MongoServer // so it can be used while connecting to a MongoServer internal CommandResult RunCommand( string databaseName, QueryFlags queryFlags, CommandDocument command, bool throwOnError) { var commandName = command.GetElement(0).Name; var writerSettings = new BsonBinaryWriterSettings { GuidRepresentation = GuidRepresentation.Unspecified, MaxDocumentSize = _serverInstance.MaxDocumentSize }; using (var message = new MongoQueryMessage(writerSettings, databaseName + ".$cmd", queryFlags, 0, 1, command, null)) { SendMessage(message, null, databaseName); // write concern doesn't apply to queries } var readerSettings = new BsonBinaryReaderSettings { GuidRepresentation = GuidRepresentation.Unspecified, MaxDocumentSize = _serverInstance.MaxDocumentSize }; var reply = ReceiveMessage <BsonDocument>(readerSettings, null); if (reply.NumberReturned == 0) { var message = string.Format("Command '{0}' failed. No response returned.", commandName); throw new MongoCommandException(message); } var commandResult = new CommandResult(command, reply.Documents[0]); if (throwOnError && !commandResult.Ok) { throw new MongoCommandException(commandResult); } return(commandResult); }
// this is a low level method that doesn't require a MongoServer // so it can be used while connecting to a MongoServer internal CommandResult RunCommand( MongoServer server, string collectionName, QueryFlags queryFlags, CommandDocument command ) { var commandName = command.GetElement(0).Name; using ( var message = new MongoQueryMessage( server, collectionName, queryFlags, 0, // numberToSkip 1, // numberToReturn (must be 1 or -1 for commands) command, null // fields ) ) { SendMessage(message, SafeMode.False); } var reply = ReceiveMessage <BsonDocument>(server); if (reply.NumberReturned == 0) { var message = string.Format("Command '{0}' failed: no response returned", commandName); throw new MongoCommandException(message); } var commandResult = new CommandResult(command, reply.Documents[0]); if (!commandResult.Ok) { throw new MongoCommandException(commandResult); } return(commandResult); }
/// <summary> /// 执行数据集命令 /// </summary> /// <param name="cmdDoc"></param> /// <param name="mongoCol"></param> /// <returns></returns> public static CommandResult ExecuteMongoColCommand(CommandDocument cmdDoc, MongoCollection mongoCol) { CommandResult mCommandResult; try { mCommandResult = mongoCol.Database.RunCommand(cmdDoc); } catch (MongoCommandException ex) { mCommandResult = new CommandResult(ex.Result); } var e = new RunCommandEventArgs { CommandString = cmdDoc.GetElement(0).Value.ToString(), RunLevel = EnumMgr.PathLevel.Database, Result = mCommandResult }; OnCommandRunComplete(e); return(mCommandResult); }