Example usage for com.mongodb DBCollection drop

List of usage examples for com.mongodb DBCollection drop

Introduction

In this page you can find the example usage for com.mongodb DBCollection drop.

Prototype

public void drop() 

Source Link

Document

Drops (deletes) this collection from the database.

Usage

From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java

License:Open Source License

public static void clearmongodb()

{
    try {//from   w w  w.j  ava2 s .c o m

        MongoClient mongoClient = new MongoClient(mdlocalhost, mdport);
        DB db = mongoClient.getDB(tmtextdb);
        boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray());

        DBCollection collection1 = db.getCollection("entity");
        collection1.drop();
        DBCollection collection2 = db.getCollection("attribute");
        collection2.drop();
        DBCollection collection3 = db.getCollection("deepmodel");
        collection3.drop();
        DBCollection collection4 = db.getCollection("level");
        collection4.drop();
        DBCollection collection5 = db.getCollection("binaryconnection");
        collection5.drop();
        DBCollection collection6 = db.getCollection("inheritanceparticipation");
        collection6.drop();
        DBCollection collection7 = db.getCollection("inheritancerelationship");
        collection7.drop();
        DBCollection collection8 = db.getCollection("method");
        collection8.drop();
        DBCollection collection9 = db.getCollection("package");
        collection9.drop();
        DBCollection collection10 = db.getCollection("generalconnection");
        collection10.drop();
        DBCollection collection11 = db.getCollection("participation");
        collection11.drop();

        //            DBCursor cursor = collection.find();              
        //            while(cursor.hasNext()) {
        //                      System.out.println(cursor.next());
        //                 }          
        //            collection.drop();

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:edu.csulaerp.db.ReferenceMongo.java

License:Apache License

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes no args/*from w w w .j  a  v  a  2s .  c o  m*/
 * @throws UnknownHostException if it cannot connect to a MongoDB instance at localhost:27017
 */
public static void main(final String[] args) throws UnknownHostException {
    // connect to the local database server
    MongoClient mongoClient = new MongoClient();

    /*
    // Authenticate - optional
    MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database, password);
    MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));
    */

    // get handle to "mydb"
    DB db = mongoClient.getDB("mydb");

    // get a list of the collections in this database and print them out
    Set<String> collectionNames = db.getCollectionNames();
    for (final String s : collectionNames) {
        System.out.println(s);
    }

    // get a collection object to work with
    DBCollection coll = db.getCollection("testCollection");

    // drop all the data in it
    coll.drop();

    // make a document and insert it
    BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1)
            .append("info", new BasicDBObject("x", 203).append("y", 102));

    coll.insert(doc);

    // get it (since it's the only one in there since we dropped the rest earlier on)
    DBObject myDoc = coll.findOne();
    System.out.println(myDoc);

    // now, lets add lots of little documents to the collection so we can explore queries and cursors
    for (int i = 0; i < 100; i++) {
        coll.insert(new BasicDBObject().append("i", i));
    }
    System.out
            .println("total # of documents after inserting 100 small ones (should be 101) " + coll.getCount());

    // lets get all the documents in the collection and print them out
    DBCursor cursor = coll.find();
    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // now use a query to get 1 document out
    BasicDBObject query = new BasicDBObject("i", 71);
    cursor = coll.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // $ Operators are represented as strings
    query = new BasicDBObject("j", new BasicDBObject("$ne", 3)).append("k", new BasicDBObject("$gt", 10));

    cursor = coll.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // now use a range query to get a larger subset
    // find all where i > 50
    query = new BasicDBObject("i", new BasicDBObject("$gt", 50));
    cursor = coll.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // range query with multiple constraints
    query = new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30));
    cursor = coll.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // Count all documents in a collection but take a maximum second to do so
    coll.find().maxTime(1, SECONDS).count();

    // Bulk operations
    BulkWriteOperation builder = coll.initializeOrderedBulkOperation();
    builder.insert(new BasicDBObject("_id", 1));
    builder.insert(new BasicDBObject("_id", 2));
    builder.insert(new BasicDBObject("_id", 3));

    builder.find(new BasicDBObject("_id", 1)).updateOne(new BasicDBObject("$set", new BasicDBObject("x", 2)));
    builder.find(new BasicDBObject("_id", 2)).removeOne();
    builder.find(new BasicDBObject("_id", 3)).replaceOne(new BasicDBObject("_id", 3).append("x", 4));

    BulkWriteResult result = builder.execute();
    System.out.println("Ordered bulk write result : " + result);

    // Unordered bulk operation - no guarantee of order of operation
    builder = coll.initializeUnorderedBulkOperation();
    builder.find(new BasicDBObject("_id", 1)).removeOne();
    builder.find(new BasicDBObject("_id", 2)).removeOne();

    result = builder.execute();
    System.out.println("Ordered bulk write result : " + result);

    // parallelScan
    ParallelScanOptions parallelScanOptions = ParallelScanOptions.builder().numCursors(3).batchSize(300)
            .build();

    List<Cursor> cursors = coll.parallelScan(parallelScanOptions);
    for (Cursor pCursor : cursors) {
        while (pCursor.hasNext()) {
            System.out.println(pCursor.next());
        }
    }

    // release resources
    db.dropDatabase();
    mongoClient.close();
}

From source file:edu.sjsu.carbonated.client.MongoDBDOA.java

License:Apache License

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

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

    // get handle to "mydb"
    DB db = m.getDB("mydb");

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

    // get a list of the collections in this database and print them out
    Set<String> colls = db.getCollectionNames();
    for (String s : colls) {
        System.out.println(s);/*w  ww.  j  a  v a 2  s.c  o m*/
    }

    // get a collection object to work with
    DBCollection coll = db.getCollection("testCollection");

    // drop all the data in it
    coll.drop();

    // make a document and insert it
    BasicDBObject doc = new BasicDBObject();

    doc.put("test", new AlbumResource("name", "desc", "user", "asdf"));
    doc.put("name", "MongoDB");
    doc.put("type", "database");
    doc.put("count", 1);

    BasicDBObject info = new BasicDBObject();

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

    doc.put("info", info);

    coll.insert(doc);

    // get it (since it's the only one in there since we dropped the rest earlier on)
    DBObject myDoc = coll.findOne();
    System.out.println(myDoc);

    // now, lets add lots of little documents to the collection so we can explore queries and cursors
    for (int i = 0; i < 100; i++) {
        coll.insert(new BasicDBObject().append("i", i));
    }
    System.out
            .println("total # of documents after inserting 100 small ones (should be 101) " + coll.getCount());

    //  lets get all the documents in the collection and print them out
    DBCursor cur = coll.find();
    while (cur.hasNext()) {
        System.out.println(cur.next());
    }

    //  now use a query to get 1 document out
    BasicDBObject query = new BasicDBObject();
    query.put("i", 71);
    cur = coll.find(query);

    while (cur.hasNext()) {
        System.out.println(cur.next());
    }

    //  now use a range query to get a larger subset
    query = new BasicDBObject();
    query.put("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50
    cur = coll.find(query);

    while (cur.hasNext()) {
        System.out.println(cur.next());
    }

    // range query with multiple contstraings
    query = new BasicDBObject();
    query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e.   20 < i <= 30
    cur = coll.find(query);

    while (cur.hasNext()) {
        System.out.println(cur.next());
    }

    // create an index on the "i" field
    coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending

    //  list the indexes on the collection
    List<DBObject> list = coll.getIndexInfo();
    for (DBObject o : list) {
        System.out.println(o);
    }

    // See if the last operation had an error
    System.out.println("Last error : " + db.getLastError());

    // see if any previous operation had an error
    System.out.println("Previous error : " + db.getPreviousError());

    // force an error
    db.forceError();

    // See if the last operation had an error
    System.out.println("Last error : " + db.getLastError());

    db.resetError();
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static void DropCol(String colName, MongoClient mongoClient) {
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection myCollection = db.getCollection(colName);
    myCollection.drop();
    //mongoClient.close();
}

From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java

License:EUPL

/**
 * Runs a map-reduce aggregation over a collection and saves the result to a temporary collection, which is deleted at the end
 * of the execution. The results of the operation are returned to the caller in a list of {@code BasicDBObject}.
 * @param collection - collection whose objects are searched
 * @param mapFn - map function//from  w ww.j  a v a  2s . co  m
 * @param reduceFn - reduce function
 * @param query - (optional) specifies the selection criteria using query operators for determining the documents input to the 
 *        map function. Set this parameter to {@code null} to use all documents in the collection
 * @return a list of {@code BasicDBObject} which contains the results of this map reduce operation.
 * @see <a href="http://cookbook.mongodb.org/patterns/pivot/">The MongoDB Cookbook - Pivot Data with Map reduce</a>
 */
public List<BasicDBObject> mapReduce(final String collection, final String mapFn, final String reduceFn,
        final @Nullable DBObject query) {
    checkArgument(isNotBlank(collection), "Uninitialized or invalid collection");
    checkArgument(isNotBlank(mapFn), "Uninitialized or map function");
    checkArgument(isNotBlank(reduceFn), "Uninitialized or reduce function");
    final List<BasicDBObject> list = newArrayList();
    final DB db = client().getDB(CONFIG_MANAGER.getDbName());
    final DBCollection dbcol = db.getCollection(collection);
    final DBCollection tmpCol = tmpCollection();
    try {
        final MapReduceCommand command = new MapReduceCommand(dbcol, mapFn, reduceFn, tmpCol.getName(), REDUCE,
                query);
        final MapReduceOutput output = dbcol.mapReduce(command);
        final Iterable<DBObject> results = output.results();
        for (final DBObject result : results) {
            list.add((BasicDBObject) result);
        }
    } catch (Exception e) {
        throw new IllegalStateException("Failed to execute map-reduce operation", e);
    } finally {
        try {
            tmpCol.drop();
        } catch (Exception mdbe) {
            LOGGER.warn("Failed to drop temporary collection", mdbe);
        }
    }
    return list;
}

From source file:example.QuickTour.java

License:Apache License

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes no args//from  ww w . j  a  v a  2 s  . com
 * @throws UnknownHostException if it cannot connect to a MongoDB instance at localhost:27017
 */
public static void main(final String[] args) throws UnknownHostException {
    // connect to the local database server
    MongoClient mongoClient = new MongoClient();

    // get handle to "mydb"
    DB db = mongoClient.getDB("mydb");

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

    // get a list of the collections in this database and print them out
    Set<String> collectionNames = db.getCollectionNames();
    for (final String s : collectionNames) {
        System.out.println(s);
    }

    // get a collection object to work with
    DBCollection testCollection = db.getCollection("testCollection");

    // drop all the data in it
    testCollection.drop();

    // make a document and insert it
    BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1)
            .append("info", new BasicDBObject("x", 203).append("y", 102));

    testCollection.insert(doc);

    // get it (since it's the only one in there since we dropped the rest earlier on)
    DBObject myDoc = testCollection.findOne();
    System.out.println(myDoc);

    // now, lets add lots of little documents to the collection so we can explore queries and cursors
    for (int i = 0; i < 100; i++) {
        testCollection.insert(new BasicDBObject().append("i", i));
    }
    System.out.println(
            "total # of documents after inserting 100 small ones (should be 101) " + testCollection.getCount());

    // lets get all the documents in the collection and print them out
    DBCursor cursor = testCollection.find();
    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // now use a query to get 1 document out
    BasicDBObject query = new BasicDBObject("i", 71);
    cursor = testCollection.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // now use a range query to get a larger subset
    query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50
    cursor = testCollection.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // range query with multiple constraints
    query = new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e.   20 < i <= 30
    cursor = testCollection.find(query);

    try {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }

    // create an index on the "i" field
    testCollection.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending

    // list the indexes on the collection
    List<DBObject> list = testCollection.getIndexInfo();
    for (final DBObject o : list) {
        System.out.println(o);
    }

    // See if the last operation had an error
    System.out.println("Last error : " + db.getLastError());

    // see if any previous operation had an error
    System.out.println("Previous error : " + db.getPreviousError());

    // force an error
    db.forceError();

    // See if the last operation had an error
    System.out.println("Last error : " + db.getLastError());

    db.resetError();

    // release resources
    mongoClient.close();
}

From source file:examples.QuickTour.java

License:Apache License

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

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

    // get handle to "mydb"
    DB db = m.getDB("mydb");

    // Authenticate - optional
    boolean auth = db.authenticate("foo", new char[] { 'b', 'a', 'r' });

    // get a list of the collections in this database and print them out
    Set<String> colls = db.getCollectionNames();
    for (String s : colls) {
        System.out.println(s);//from   ww  w  . ja v a2s.co m
    }

    // get a collection object to work with
    DBCollection coll = db.getCollection("testCollection");

    // drop all the data in it
    coll.drop();

    // make a document and insert it
    BasicDBObject doc = new BasicDBObject();

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

    BasicDBObject info = new BasicDBObject();

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

    doc.put("info", info);

    coll.insert(doc);

    // get it (since it's the only one in there since we dropped the rest earlier on)
    DBObject myDoc = coll.findOne();
    System.out.println(myDoc);

    // now, lets add lots of little documents to the collection so we can explore queries and cursors
    for (int i = 0; i < 100; i++) {
        coll.insert(new BasicDBObject().append("i", i));
    }
    System.out
            .println("total # of documents after inserting 100 small ones (should be 101) " + coll.getCount());

    //  lets get all the documents in the collection and print them out
    DBCursor cur = coll.find();
    while (cur.hasNext()) {
        System.out.println(cur.next());
    }

    //  now use a query to get 1 document out
    BasicDBObject query = new BasicDBObject();
    query.put("i", 71);
    cur = coll.find(query);

    while (cur.hasNext()) {
        System.out.println(cur.next());
    }

    //  now use a range query to get a larger subset
    query = new BasicDBObject();
    query.put("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50
    cur = coll.find(query);

    while (cur.hasNext()) {
        System.out.println(cur.next());
    }

    // range query with multiple contstraings
    query = new BasicDBObject();
    query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e.   20 < i <= 30
    cur = coll.find(query);

    while (cur.hasNext()) {
        System.out.println(cur.next());
    }

    // create an index on the "i" field
    coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending

    //  list the indexes on the collection
    List<DBObject> list = coll.getIndexInfo();
    for (DBObject o : list) {
        System.out.println(o);
    }

    // See if the last operation had an error
    System.out.println("Last error : " + db.getLastError());

    // see if any previous operation had an error
    System.out.println("Previous error : " + db.getPreviousError());

    // force an error
    db.forceError();

    // See if the last operation had an error
    System.out.println("Last error : " + db.getLastError());

    db.resetError();
}

From source file:ezbake.locksmith.db.MongoDBService.java

License:Apache License

public boolean dropCollectionFromDb(String collection) {
    DBCollection coll = db.getCollection(collection);
    coll.drop();

    return true;// w ww  . j  a v a2  s .c om
}

From source file:field_sum.Field_sum.java

/**
 * @param args the command line arguments
 * @throws java.net.UnknownHostException
 *//*from www . ja  v  a  2s.  c  o m*/
public static void main(String[] args) throws UnknownHostException {

    MongoClient client = new MongoClient("localhost", 27017);
    DB myDB = client.getDB("m101");
    DBCollection collection = myDB.getCollection("Sample3");

    /**
     * random_number field generates random numbers 
    */
    Random random_numbers = new Random();

    collection.drop();

    DBObject query = QueryBuilder.start("x").greaterThan(10).lessThan(70).and("y").greaterThan(10).lessThan(70)
            .get();

    for (int i = 0; i < 100; i++) {
        collection.insert(new BasicDBObject("x", random_numbers.nextInt(100))
                .append("y", random_numbers.nextInt(100)).append("z", random_numbers.nextInt(200)));
    }

    DBCursor cursor = collection.find(query);
    int sum = 0;

    try {
        while (cursor.hasNext()) {
            DBObject dBObject = cursor.next();
            sum += (int) dBObject.get("x");
            System.out.println(dBObject.get("x"));
        }
    } finally {
        cursor.close();
    }
    System.out.println(sum);

}

From source file:fr.cirad.web.controller.gigwa.base.AbstractVariantController.java

License:Open Source License

/**
 * Gets the temporary variant collection.
 *
 * @param sModule the module//from  w  w w  .  j a va 2  s.c om
 * @param processID the process id
 * @param fEmptyItBeforeHand whether or not to empty it beforehand
 * @return the temporary variant collection
 */
private DBCollection getTemporaryVariantCollection(String sModule, String processID,
        boolean fEmptyItBeforeHand) {
    DBCollection coll = MongoTemplateManager.get(sModule)
            .getCollection(MongoTemplateManager.TEMP_COLL_PREFIX + Helper.convertToMD5(processID));
    if (fEmptyItBeforeHand)
        coll.drop();
    return coll;
}