Example usage for com.mongodb Mongo dropDatabase

List of usage examples for com.mongodb Mongo dropDatabase

Introduction

In this page you can find the example usage for com.mongodb Mongo dropDatabase.

Prototype

public void dropDatabase(final String dbName) 

Source Link

Document

Drops the database if it exists.

Usage

From source file:com.github.niltz.maven.plugins.mongodb.AbstractMongoDBMojo.java

License:Open Source License

/**
 * Drops the configured mongo database./*from  w  w w  .j ava 2 s. c  o m*/
 * 
 * @param mongo
 *            the mogno
 */
protected void dropDatabase(ConnectionSettings connectionSettings, Mongo mongo) {
    mongo.dropDatabase(connectionSettings.getDatabase());
}

From source file:com.streamreduce.AbstractServiceTestCase.java

License:Apache License

@After
public void tearDown() throws Exception {
    Mongo mongo = applicationManager.getMessageDBDatastore().getMongo();
    mongo.dropDatabase("TEST_nodeabledb");
    mongo.dropDatabase("TEST_nodeablemsgdb");
}

From source file:es.devcircus.mongodb_examples.hello_world.Main.java

License:Open Source License

/**
 * Mtodo en el que se muestran algunas funciones de administracin.
 *///from  ww w .j  a va2 s.  c o  m
public static void quickTourOfTheAdministrativeFunctions() {
    try {
        System.out.println();
        System.out.println("---------------------------------------------------------------");
        System.out.println(" Quick Tour of the Administrative Functions                    ");
        System.out.println("---------------------------------------------------------------");
        System.out.println();

        /*Quick Tour of the Administrative Functions
         Getting A List of Databases
         You can get a list of the available databases:*/

        Mongo m = new Mongo();

        for (String s : m.getDatabaseNames()) {
            System.out.println(" - " + s);
        }

        /*Dropping A Database
         You can drop a database by name using the Mongo object:*/

        m.dropDatabase(DB_NAME);
    } catch (UnknownHostException | MongoException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:eu.cassandra.server.threads.RunsContextListener.java

License:Apache License

@Override
public void contextInitialized(ServletContextEvent arg0) {
    ServletContext context = arg0.getServletContext();
    runs = new HashMap<String, Future<?>>();
    context.setAttribute("MY_RUNS", runs);
    // Delete all unfinished thread from db and their dbs:
    DBObject query = new BasicDBObject();
    query.put("ended", -1);
    DBCursor cursor = DBConn.getConn().getCollection(MongoRuns.COL_RUNS).find(query);
    while (cursor.hasNext()) {
        DBObject run = cursor.next();/*from w  ww.ja  v  a 2  s . c  o m*/
        DBConn.getConn().getCollection(MongoRuns.COL_RUNS).remove(run);
        String run_id = run.get("_id").toString();
        Mongo mongo;
        try {
            mongo = new Mongo();
            mongo.dropDatabase(run_id);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }
    }
}

From source file:examples.QuickTourAdmin.java

License:Apache License

public static void main(String[] args) throws Exception {

    // connect to the local database server 
    Mongo m = new Mongo();

    // Authenticate - optional
    // boolean auth = db.authenticate("foo", "bar");

    // get db names
    for (String s : m.getDatabaseNames()) {
        System.out.println(s);/*from   ww w  .j av  a2  s. c  o m*/
    }

    // get a db
    DB db = m.getDB("com_mongodb_MongoAdmin");

    // do an insert so that the db will really be created.  Calling getDB() doesn't really take any
    // action with the server 
    db.getCollection("testcollection").insert(new BasicDBObject("i", 1));
    for (String s : m.getDatabaseNames()) {
        System.out.println(s);
    }

    // drop a database
    m.dropDatabase("com_mongodb_MongoAdmin");

    for (String s : m.getDatabaseNames()) {
        System.out.println(s);
    }
}

From source file:net.sf.okapi.lib.tmdb.mongodb.Repository.java

License:Open Source License

/**
 * Helper method to delete existing repository
 * @param repoName// w w  w  .  j a v  a  2 s .  com
 */
static public void delete(String repoName) {
    try {
        Mongo mDel = new Mongo();
        mDel.dropDatabase(repoName);
        mDel.close();
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    } catch (MongoException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.isis.objectstore.nosql.db.mongo.DemoMongo.java

License:Apache License

public void installed() throws Exception {

    final Mongo m = new Mongo();

    for (final String s : m.getDatabaseNames()) {
        System.out.println(s);/*from ww  w.j av a  2s .c o  m*/
    }

    /*
     * Mongo m = new Mongo( "localhost" ); Mongo m = new Mongo( "localhost"
     * , 27017 );
     */
    m.dropDatabase("mydb");

    System.out.println("\n...");
    for (final String s : m.getDatabaseNames()) {
        System.out.println(s);
    }

    final DB db = m.getDB("mydb");
    /*
     * DBCollection coll = db.getCollection("testCollection1"); coll =
     * db.getCollection("testCollection2");
     */

    final DBCollection coll = db.getCollection("testCollection1");

    final BasicDBObject doc = new BasicDBObject();

    doc.put("name", "MongoDB");
    doc.put("type", "database");
    doc.put("count", 1);

    final BasicDBObject info = new BasicDBObject();

    info.put("x", 203);
    info.put("y", 102);

    doc.put("info", info);

    coll.insert(doc);

    final Set<String> colls = db.getCollectionNames();

    for (final String s : colls) {
        System.out.println(s);
    }

}

From source file:org.hbird.business.systemtest.Finisher.java

License:Apache License

public void process(Exchange exchange) throws MalformedObjectNameException, MalformedURLException,
        NullPointerException, IOException, Exception {

    LOG.info(/*from  w  ww . jav  a2 s. com*/
            "------------------------------------------------------------------------------------------------------------");
    LOG.info("Starting");

    LOG.info("System Test done.");

    LOG.info("Purging all activemq topics and queues.");

    /* Check that the antenna schedule has been filled. */
    IQueueManager api = new QueueManagerApi();

    for (String queueName : api.listQueues()) {
        LOG.info(" - Purging queue '" + queueName + "'.");
        api.clearQueue(queueName);
    }

    for (String topicName : api.listTopics()) {
        LOG.info(" - Purging topic '" + topicName + "'.");
        api.clearTopic(topicName);
    }

    Mongo mongo = getContext().getRegistry().lookup("mongo", Mongo.class);
    mongo.dropDatabase("hbird_test");

    Thread.sleep(2000);

    LOG.info("Ciao!");
    LOG.info(
            "------------------------------------------------------------------------------------------------------------");

    System.exit(1);
}

From source file:org.iternine.jeppetto.testsupport.db.MongoDatabase.java

License:Apache License

@Override
public void close() {
    if (mongoDbName == null) {
        return;/* w w w.j a va  2 s  . co  m*/
    }

    try {
        Mongo mongo = new Mongo("127.0.0.1", mongoDbPort);
        DB db = mongo.getDB(mongoDbName);

        db.resetError();
        db.dropDatabase();

        DBObject err = db.getLastError();
        if (err != null && err.get("err") != null) {
            logger.error("Could not drop database {}: {}", mongoDbName, err);
        }

        mongo.dropDatabase(mongoDbName);

        if (mongo.getDatabaseNames().contains(mongoDbName)) {
            logger.error("Database {} will not go away!", mongoDbName);
        }
    } catch (UnknownHostException e) {
        // weird
    } catch (MongoException e) {
        logger.warn("Could not drop database {}: {}", mongoDbName, e.getMessage());
    }
}

From source file:org.mediawiki.importer.XmlDumpReader.java

License:Open Source License

/**
 * Initialize a processor for a MediaWiki XML dump stream.
 * Events are sent to a single DumpWriter output sink, but you
 * can chain multiple output processors with a MultiWriter.
 * @param inputStream Stream to read XML from.
 * @param writer Output sink to send processed events to.
 *///from   w  w w  .j  a  v  a2s .  c o  m
public XmlDumpReader(InputStream inputStream, DumpWriter writer) {
    input = inputStream;
    this.writer = writer;
    buffer = new char[4096];
    len = 0;
    hasContent = false;

    //by philipp.staender
    try {
        //open connection to monogdb an get collections
        Mongo m = new Mongo();
        System.out.println("Initialisiere mongodb Datenbank '" + this.mongodbDatabasename + "'");
        m.dropDatabase(this.mongodbDatabasename);
        DB db = m.getDB(this.mongodbDatabasename);
        System.out.println("Setze Indizes in mongodb collections...");
        this.mongodbArticles = db.getCollection("articles");
        this.mongodbArticles.ensureIndex("title");
        if (XmlDumpReader.generateTextIndizes) {
            this.mongodbTextindexes = db.getCollection("textindex");
            this.mongodbTextindexes.ensureIndex("title");
            this.mongodbTextindexes.ensureIndex("article");
            this.mongodbTextindexes.ensureIndex("links");
            this.mongodbTextindexes.ensureIndex("sections.subtitle");
        }
    } catch (Exception e) {
        System.err.println("Fehler beim Initialisieren  der mongodb: " + e.getMessage());
        System.err.println("Applikation wird beendet");
        System.exit(0);
    }
    //open connection to mysql
    String dbURL = "jdbc:mysql://" + this.mysqlHost + ":" + this.mysqlPort + "/" + this.mysqlDatabase;
    try {
        this.mysqlConnection = (Connection) DriverManager.getConnection(dbURL, this.mysqlUsername,
                this.mysqlPassword);
        //Prepare tables for database
        System.out.println("Initialisiere notwendige Tabellen in mysql Datenbank '" + this.mysqlDatabase + "'");
        Statement stmt = (Statement) this.mysqlConnection.createStatement();
        String sql = "DROP TABLE IF EXISTS `articles`;";
        stmt.executeUpdate(sql);
        sql = "CREATE TABLE IF NOT EXISTS `articles` (  `ID` int(11) NOT NULL AUTO_INCREMENT,  `MongoID` varchar(255) NOT NULL,  `Title` varchar(255) NOT NULL,  `Redirect` varchar(255) NOT NULL,  `Comment` text NOT NULL,  `Content` longtext NOT NULL, `Links` text NOT NULL, PRIMARY KEY (`ID`),  KEY `ArticleTitle` (`Title`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
        stmt.executeUpdate(sql);
        if (XmlDumpReader.generateTextIndizes) {
            sql = "DROP TABLE IF EXISTS `textindex`;";
            stmt.executeUpdate(sql);
            sql = "CREATE TABLE IF NOT EXISTS `textindex` (  `ID` int(11) NOT NULL AUTO_INCREMENT,  `ArticleID` int(11) NOT NULL,  `MongoID` varchar(255) DEFAULT NULL,  `Sort` int(11) NOT NULL, `Title` varchar(255) DEFAULT NULL,  `Text` longtext,  `Links` text NOT NULL,  PRIMARY KEY (`ID`),  KEY `ArticleID` (`ArticleID`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
            stmt.executeUpdate(sql);
        }
    } catch (SQLException ex) {
        // handle any errors
        System.err.println("Fehler beim Initialisieren der mysql Datenbank...");
        System.err.println("SQLException: " + ex.getMessage());
        System.err.println("SQLState: " + ex.getSQLState());
        System.err.println("VendorError: " + ex.getErrorCode());
        System.err.println("Applikation wird beendet");
        System.exit(0);
    }
}