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:org.opentestsystem.delivery.AccValidator.handlers.ValidationHandler.java

License:Open Source License

/**
 * @param resourceFamilies/*ww  w  .j a  v a2s. c o  m*/
 */
public void storeResourceFamiliesIntoDB(List<ResourceFamily> resourceFamilies) throws StoringException {

    DB database = getMongoDBConnection();
    DBCollection collection = database.getCollection("resourceFamily");

    collection.drop();

    String name = null;
    String language = null;
    String label = null;
    String message = null;
    String description = null;
    String code = null;
    Integer order = null;
    String defaultSelection = null;
    String resourceType = null;
    Boolean mutuallyExclusive;
    Boolean disabled;

    for (ResourceFamily rf : resourceFamilies) {
        BasicDBObject resourceFamily = new BasicDBObject();
        BasicDBList subjects = new BasicDBList();

        List<AccFamilySubject> accFamilySubjects = rf.getSubject();

        for (int i = 0; i < accFamilySubjects.size(); i++) {
            BasicDBObject subject = new BasicDBObject();
            AccFamilySubject sub = accFamilySubjects.get(i);
            code = sub.getCode();
            if (code != null)
                subject.append("code", code);

            name = sub.getName();
            if (name != null)
                subject.append("name", name);

            subjects.add(subject);
        }

        BasicDBList grade = new BasicDBList();
        List<String> dbGrades = rf.getGrade();
        for (int j = 0; j < dbGrades.size(); j++) {
            grade.add(dbGrades.get(j).toString());

        }
        List<MasterResourceAccommodation> accommodations = rf.getMasterResourceAccommodation();
        BasicDBList masterResourceAccommodation = new BasicDBList();
        for (int k = 0; k < accommodations.size(); k++) {
            MasterResourceAccommodation mracc = accommodations.get(k);
            BasicDBObject accommodation = new BasicDBObject();
            BasicDBObject header = new BasicDBObject();
            BasicDBList headersList = new BasicDBList();
            BasicDBList options = new BasicDBList();
            List<AccommodationText> headerTexts = mracc.getHeader();
            if (headerTexts != null) {
                for (AccommodationText headerText : headerTexts) {
                    if (headerText != null) {
                        language = headerText.getLanguage();
                        if (language != null)
                            header.append("language", language);

                        label = headerText.getLabel();
                        if (label != null)
                            header.append("label", label);

                        message = headerText.getMessage();
                        if (message != null)
                            header.append("message", message);

                        description = headerText.getDescription();
                        if (description != null)
                            header.append("description", description);

                        headersList.add(header);
                    }
                }
            }
            code = mracc.getCode();
            if (code != null)
                accommodation.append("code", code);

            order = mracc.getOrder();
            if (order != 0)
                accommodation.append("order", order);

            defaultSelection = mracc.getDefaultSelection();
            if (defaultSelection != null)
                accommodation.append("defaultSelection", defaultSelection);

            if (header.size() > 0)
                accommodation.append("header", headersList);

            resourceType = mracc.getResourceType();
            if (mracc.getResourceType() != null)
                accommodation.append("resourceType", resourceType);

            disabled = mracc.isDisabled();
            if (mracc.isDisabled() != false) {
                accommodation.append("disabled", disabled);
            }

            List<AccommodationOption> accOptions = mracc.getOptions();
            for (int i = 0; i < accOptions.size(); i++) {
                AccommodationOption accOption = accOptions.get(i);

                if (accOption != null) {
                    BasicDBObject option = new BasicDBObject();
                    code = accOption.getCode();
                    if (code != null)
                        option.append("code", code);

                    order = accOption.getOrder();
                    if (order != null && Integer.valueOf(order) != 0)
                        option.append("order", order);

                    mutuallyExclusive = accOption.isMutuallyExclusive();
                    if (mutuallyExclusive != false)
                        option.append("mutuallyExclusive", mutuallyExclusive);
                    BasicDBList textList = new BasicDBList();
                    List<AccommodationText> accTexts = accOption.getText();
                    if (accTexts != null) {
                        for (AccommodationText accText : accTexts) {
                            if (accText != null) {
                                BasicDBObject text = new BasicDBObject();

                                language = accText.getLanguage();
                                if (language != null)
                                    text.append("language", language);

                                label = accText.getLabel();
                                if (label != null)
                                    text.append("label", label);

                                description = accText.getDescription();
                                if (description != null)
                                    text.append("description", description);

                                message = accText.getMessage();
                                if (message != null)
                                    text.append("message", message);
                                textList.add(text);

                            }
                            if (textList.size() > 0)
                                option.append("text", textList);
                        }
                    }
                    if (option.size() > 0)
                        options.add(option);
                }
            }

            if (options.size() > 0)
                accommodation.append("options", options);

            if (accommodation.size() > 0)
                masterResourceAccommodation.add(accommodation);
        }

        resourceFamily.append("subject", subjects);
        resourceFamily.append("grade", grade);
        resourceFamily.append("masterResourceAccommodation", masterResourceAccommodation);

        collection.insert(resourceFamily);
    }

}

From source file:org.opentestsystem.delivery.AccValidator.handlers.ValidationHandler.java

License:Open Source License

/**
 * @param masterResourceAccommodations/*from   ww w.  ja v  a  2 s.  co m*/
 */
public void storeMasterResourceAccommodationIntoDB(
        List<MasterResourceAccommodation> masterResourceAccommodations) throws StoringException {

    String language = null;
    String label = null;
    String message = null;
    String description = null;
    String code = null;
    Integer order = null;
    String defaultSelection = null;
    String resourceType = null;
    Boolean mutuallyExclusive;

    try {

        DB database = getMongoDBConnection();
        DBCollection collection = database.getCollection("masterResourceAccommodation");

        collection.drop();
        for (MasterResourceAccommodation mracc : masterResourceAccommodations) {
            BasicDBObject accommodation = new BasicDBObject();
            BasicDBList headers = new BasicDBList();
            BasicDBObject header = new BasicDBObject();
            BasicDBList options = new BasicDBList();
            List<AccommodationText> headerTexts = mracc.getHeader();
            if (headerTexts != null) {
                for (AccommodationText headerText : headerTexts) {
                    language = headerText.getLanguage();
                    if (language != null)
                        header.append("language", language);

                    label = headerText.getLabel();
                    if (label != null)
                        header.append("label", label);

                    message = headerText.getMessage();
                    if (message != null)
                        header.append("message", message);

                    description = headerText.getDescription();
                    if (description != null)
                        header.append("description", description);
                    headers.add(header);
                }
            }
            code = mracc.getCode();
            if (code != null)
                accommodation.append("code", code);

            order = mracc.getOrder();
            if (order != null && Integer.valueOf(order) != 0)
                accommodation.append("order", order);

            defaultSelection = mracc.getDefaultSelection();
            if (defaultSelection != null)
                accommodation.append("defaultSelection", defaultSelection);

            accommodation.append("header", headers);

            resourceType = mracc.getResourceType();
            if (mracc.getResourceType() != null)
                accommodation.append("resourceType", resourceType);

            List<AccommodationOption> accOptions = mracc.getOptions();

            for (int i = 0; i < accOptions.size(); i++) {
                AccommodationOption accOption = accOptions.get(i);
                BasicDBObject option = new BasicDBObject();
                code = accOption.getCode();
                if (code != null)
                    option.append("code", code);

                order = accOption.getOrder();
                if (order != null)
                    option.append("order", order);

                mutuallyExclusive = accOption.isMutuallyExclusive();
                if (mutuallyExclusive != false)
                    option.append("mutuallyExclusive", mutuallyExclusive);

                List<AccommodationText> accTexts = accOption.getText();
                if (accTexts != null) {
                    BasicDBList textList = new BasicDBList();

                    for (AccommodationText accText : accTexts) {
                        BasicDBObject text = new BasicDBObject();

                        language = accText.getLanguage();
                        if (language != null)
                            text.append("language", language);

                        label = accText.getLabel();
                        if (label != null)
                            text.append("label", label);

                        description = accText.getDescription();
                        if (description != null)
                            text.append("description", description);

                        message = accText.getMessage();
                        if (message != null)
                            text.append("message", message);

                        textList.add(text);

                    }
                    if (textList.size() > 0)
                        option.append("text", textList);
                }

                if (option.size() > 0)
                    options.add(option);
            }

            if (options.size() > 0)
                accommodation.append("options", options);

            if (accommodation != null)
                collection.insert(accommodation);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.sipfoundry.sipxconfig.admin.commserver.imdb.ReplicationManagerImpl.java

License:Contributor Agreement License

@Override
public void registerTunnels(Location location) {
    DBCollection registrarNode = m_imdb.getDb().getCollection(MongoConstants.STUNNEL_COLLECTION);
    registrarNode.drop();

    Location[] locations = m_locationsManager.getLocations();
    if (locations.length > 1) {
        for (int i = 0; i < locations.length; i++) {
            List<Location> otherLocations = new ArrayList<Location>(Arrays.asList(locations));
            otherLocations.remove(locations[i]);

            for (TunnelProvider tunnelProvider : m_tunnelManager.getTunnelProviders()) {
                List<RemoteOutgoingTunnel> tunnels = (List<RemoteOutgoingTunnel>) tunnelProvider
                        .getClientSideTunnels(otherLocations, location);
                for (RemoteOutgoingTunnel tunnel : tunnels) {
                    DBObject search = new BasicDBObject();
                    search.put(ID, tunnel.getName());
                    DBObject server = registrarNode.findOne(search);
                    if (server == null) {
                        server = new BasicDBObject();
                        server.put(ID, tunnel.getName());
                    }/*from  w  ww.  java  2 s. c o  m*/
                    server.put(SERVER, "localhost:" + tunnel.getLocalhostPort());
                    server.put(INTERNAL_ADDRESS, tunnel.getRemoteMachineAddress());
                    Location loc = m_locationsManager.getLocationByAddress(tunnel.getRemoteMachineAddress());
                    server.put(ENABLED, loc.isRegistered());
                    registrarNode.save(server);
                }
            }
        }
        DBObject timestamp = new BasicDBObject();
        timestamp.put(ID, "timestamp");
        timestamp.put(TIMESTAMP, new Long(System.currentTimeMillis() / 1000).toString());
        registrarNode.save(timestamp);
    }
}

From source file:org.springframework.data.document.mongodb.MongoTemplate.java

License:Apache License

public void dropCollection(String collectionName) {

    execute(collectionName, new CollectionCallback<Void>() {
        public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            collection.drop();
            return null;
        }//from w w w .j  av  a  2 s . c  o m
    });
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

License:Apache License

public void dropCollection(String collectionName) {
    execute(collectionName, new CollectionCallback<Void>() {
        public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            collection.drop();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Dropped collection [{}]", collection.getFullName());
            }//from w  ww  . j ava 2  s .co m
            return null;
        }
    });
}

From source file:org.springframework.data.mongodb.test.util.CleanMongoDB.java

License:Apache License

private void dropCollectionsOrIndexIfRequried(DB db, Collection<String> collectionsToUse) {

    for (String collectionName : collectionsToUse) {

        if (db.collectionExists(collectionName)) {

            DBCollection collection = db.getCollectionFromString(collectionName);
            if (collection != null) {

                if (types.contains(Struct.COLLECTION)) {
                    collection.drop();
                    LOGGER.debug("Dropping collection '{}' for DB '{}'. ", collectionName, db.getName());
                } else if (types.contains(Struct.INDEX)) {
                    collection.dropIndexes();
                    LOGGER.debug("Dropping indexes in collection '{}' for DB '{}'. ", collectionName,
                            db.getName());
                }//from   ww w.  j a  v  a  2 s .  co  m
            }
        }
    }
}

From source file:parlare.application.server.model.Database.java

private String doClientMongo() {

    String print = "";

    System.out.println("User:" + user + " Source:" + source + " Password:" + password);

    try {/*w ww . j av  a2  s  .  c o m*/

        // connect to the local database server
        MongoClient mongoClient = new MongoClient(new ServerAddress(server),
                Arrays.asList(MongoCredential.createMongoCRCredential(user, source, password.toCharArray())),
                new MongoClientOptions.Builder().build());

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

        // 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 (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: " + 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 (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();

    } catch (UnknownHostException ex) {
        Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    }

    return print;

}

From source file:pt.tiago.mongodbteste.MongoDB.java

private void dropCollections() {
    for (DBCollection collectionDB : collection) {
        collectionDB.drop();
    }
}

From source file:QueryBuilderClass.Field_selection_mongo.java

/**
 * @param args the command line arguments
 * @throws java.net.UnknownHostException
 *//*from w  w  w. jav  a2  s .c  om*/
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();

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

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

    DBCursor cursor = collection.find(query, new BasicDBObject("y", true).append("_id", false));

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

}

From source file:rapture.sheet.mongodb.MongoCellStore.java

License:Open Source License

public void drop() {
    DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
    collection.drop();
    collection.dropIndex(KEY);/*  ww  w  .j a va 2  s .  co m*/
    collection.dropIndex(ROW);
    collection.dropIndex(COL);
}