final DBObject cmd = new BasicDBObject( ); cmd.put( "copydb", "1" ); cmd.put( "slaveOk", true ); cmd.put( "fromdb", "db1" ); cmd.put( "todb", "db2" ); cmd.put( "fromhost", "fromHost" ); BasicDBObject nonceCmd = new BasicDBObject( ); nonceCmd.put( "copydbgetnonce", 1 ); nonceCmd.put( "fromhost", "fromHost" ); final CommandResult nonceResult = mongo.getDB( "admin" ).command( nonceCmd ); final String nonce = nonceResult.getString( "nonce" ); final byte[ ] innerHex =( getUserName( ) + ":mongo:" + String.valueOf( getPassword( ) ) ).getBytes( ); final byte[ ] outerHex = ( nonce + getUserName( ) + Util.hexMD5( innerHex ) ).getBytes( ); cmd.put( "username", getUserName( ) ); cmd.put( "nonce", nonce ); cmd.put( "key", Util.hexMD5( outerHex ) ); final CommandResult res = mongo.getDB( "admin" ).command( cmd ); if ( !res.ok( ) ) { throw res.getException( ) ; }
throw new MojoExecutionException("Error while executing instructions from file '" + file.getName() + "': " + result.getErrorMessage(), result.getException());
throw new MojoExecutionException("Error while executing instructions from file '" + file.getName() + "': " + result.getErrorMessage(), result.getException());
Exception ex = result.getException();
logger.error("can't insert documents with error: {} ", errorMessage); logger.error("with exception", result.getException()); throw new MongoException(errorMessage);
logger.error("can't upsert documents with error: {} ", errorMessage); logger.error("with exception", result.getException()); throw new MongoException(errorMessage);
@Override public int update(String statement, Object parameter) { logger.debug("Execute 'update' mongodb command. Statement '" + statement + "'."); UpdateConfig config = (UpdateConfig) configuration.getConfig(statement); if (config == null) { throw new MongoDaoException(statement, "Update statement id '" + statement + "' not found."); } String collection = config.getCollection(); NodeEntry query = config.getQuery(); NodeEntry action = config.getAction(); DB db = factory.getDataSource().getDB(); DBCollection coll = db.getCollection(collection); Map<String, Object> q = (Map<String, Object>) query.executorNode(configuration, parameter); Map<String, Object> a = (Map<String, Object>) action.executorNode(configuration, parameter); DBObject queryDbo = new BasicDBObject(q); logger.debug("Execute 'update' mongodb command. Query '" + queryDbo + "'."); DBObject actionDbo = (a == null) ? null : new BasicDBObject(a); logger.debug("Execute 'update' mongodb command. Action '" + actionDbo + "'."); WriteResult writeResult = coll.update(queryDbo, actionDbo, false, true, WriteConcern.SAFE); CommandResult commandResult = writeResult.getLastError(WriteConcern.SAFE); if (commandResult.getException() != null) { throw new MongoDaoException(statement, "Execute 'update' mongodb command has exception. Cause: " + commandResult.getErrorMessage()); } return writeResult.getN(); }
@Override public String insert(String statement, Object parameter) { logger.debug("Execute 'insert' mongodb command. Statement '" + statement + "'."); InsertConfig config = (InsertConfig) configuration.getConfig(statement); if (config == null) { throw new MongoDaoException(statement, "Insert statement id '" + statement + "' not found."); } String collection = config.getCollection(); NodeEntry document = config.getDocument(); Entry selectKey = config.getSelectKey(); DB db = factory.getDataSource().getDB(); DBCollection coll = db.getCollection(collection); Map<String, Object> doc = (Map<String, Object>) document.executorNode(configuration, parameter); DBObject docDbo = new BasicDBObject(doc); logger.debug("Execute 'insert' mongodb command. Doc '" + docDbo + "'."); WriteResult writeResult = coll.insert(docDbo, WriteConcern.SAFE); CommandResult commandResult = writeResult.getLastError(WriteConcern.SAFE); if (commandResult.getException() != null) { throw new MongoDaoException(statement, "Execute 'insert' mongodb command has exception. Cause: " + commandResult.getErrorMessage()); } String newId = docDbo.get("_id").toString(); logger.debug("Execute 'insert' mongodb command. ObjectId is '" + newId + "'."); if (selectKey != null) { helper.setSelectKey(selectKey, newId, parameter); } return newId; }
@Override public int delete(String statement, Object parameter) { logger.debug("Execute 'delete' mongodb command. Statement '" + statement + "'."); DeleteConfig config = (DeleteConfig) configuration.getConfig(statement); if (config == null) { throw new MongoDaoException(statement, "Delete statement id '" + statement + "' not found."); } String collection = config.getCollection(); NodeEntry query = config.getQuery(); DB db = factory.getDataSource().getDB(); DBCollection coll = db.getCollection(collection); Map<String, Object> q = (Map<String, Object>) query.executorNode(configuration, parameter); DBObject queryDbo = new BasicDBObject(q); logger.debug("Execute 'delete' mongodb command. Query '" + queryDbo + "'."); WriteResult writeResult = coll.remove(queryDbo, WriteConcern.SAFE); CommandResult commandResult = writeResult.getLastError(WriteConcern.SAFE); if (commandResult.getException() != null) { throw new MongoDaoException(statement, "Execute 'delete' mongodb command has exception. Cause: " + commandResult.getErrorMessage()); } return writeResult.getN(); }