Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

In this page you can find the example usage for com.mongodb MongoClient getDB.

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:mongodb.JavaFindDistinct.java

public static void main(String[] args) {
    try {//w  ww.j  a  v a 2s  .com
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaFindDistinct.sizesOfAllWords(collection);
        JavaFindDistinct.sizesOfQWords(collection);
        JavaFindDistinct.firstLetterOfLongWords(collection);
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:mongodb.JavaFindFields.java

public static void main(String[] args) {
    try {/*from www  .  j a va 2 s .  com*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaFindFields.excludeFields(collection, new String[] {});
        JavaFindFields.includeFields(collection, new String[] { "word" });
        JavaFindFields.includeFields(collection, new String[] { "word", "stats" });
        JavaFindFields.excludeFields(collection, new String[] { "stats", "charsets" });
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:mongodb.JavaFindLimit.java

public static void main(String[] args) {
    try {//w ww.  j a  v  a 2s. c  o  m
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaFindLimit.limitResults(collection, 1);
        JavaFindLimit.limitResults(collection, 3);
        JavaFindLimit.limitResults(collection, 5);
        JavaFindLimit.limitResults(collection, 7);
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:mongodb.javaGroup.java

public static void main(String[] args) {
    try {//ww  w  . jav a 2s.  c  om
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        javaGroup.firstIsALastIsVowel(collection);
        javaGroup.firstLetterTotals(collection);
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:MongoDb.Main.java

public static void insertCategoryAndSubCategory() throws UnknownHostException, JSONException {

    long startTime = System.nanoTime();

    //Init connect mongodb
    MongoClient mongoClient = new MongoClient(MongoDb.Config.HOST, MongoDb.Config.defaultPort);
    DB db = mongoClient.getDB(MongoDb.Config.Schema);
    String colletioName = "Category";
    DBCollection collections = db.getCollection(colletioName);

    System.out.println(collections.count());
    DBCollection subCategoryCollection = db.getCollection("subCategory");

    List categoryList = Parser.getJSONCategory();
    for (int i = 0; i < categoryList.size(); i++) {

        //Insert Category
        JSONObject jsonCategoty = (JSONObject) categoryList.get(i);
        String categoryName = jsonCategoty.getString("categoryName");
        JSONObject jsonOfCatrgory = new JSONObject();
        jsonOfCatrgory.put("categoryName", categoryName);
        String _idCategory = MongoDBController.addDocumentFromJsonToJsonObject(jsonOfCatrgory, collections);
        System.out.println(_idCategory);
        if (_idCategory.length() > 5) {
            System.out.println("? insert category");
            JSONArray subCategoryArray = jsonCategoty.getJSONArray("subList");

            for (int j = 0; j < subCategoryArray.length(); j++) {
                JSONObject jsonSubCategory = subCategoryArray.getJSONObject(j);
                jsonSubCategory.put("categoryId", _idCategory);
                String _idSubCategory = MongoDBController.addDocumentFromJsonToJsonObject(jsonSubCategory,
                        subCategoryCollection);
                if (_idSubCategory.length() > 5) {
                    System.out.println("? insert subcategory");
                }//from w  ww  .ja va 2  s .co m
            }
        }
    }

}

From source file:MongoDb.MongoDBCollection.java

public static void main(String args[]) throws UnknownHostException, JSONException {
    long startTime = System.nanoTime();

    //Init//from   w w  w . ja va 2 s. c o  m
    MongoClient mongoClient = new MongoClient(MongoDb.Config.HOST, MongoDb.Config.defaultPort);
    DB db = mongoClient.getDB(MongoDb.Config.Schema);
    String colletioName = "websosanh";

    DBCollection collections = db.getCollection(colletioName);
    //MongoDBController mongDbController = new MongoDBController(mongoClient,db);
    //if(mongDbController.addACollection("")){
    for (int i = 1; i < 2; i++) {
        JSONObject json = new JSONObject();
        json.put("status", "ok");
        MongoDBController.addDocumentFromJsonToJsonObject(json, collections);
        //System.out.println(collections.getCount());
    }
    long endTime = System.nanoTime();
    long duration = (endTime - startTime);
    System.out.println(collections.getCount());
    System.out.println("Time Total excute is " + duration);

    //}

}

From source file:mongodb.performance.MongoDBPerformance.java

/**
 * @param args the command line arguments
 *///from w w w.  jav a 2s .c o  m
public static void main(String[] args) throws UnknownHostException, FileNotFoundException, IOException {
    if (args.length == 0) {
        System.out.println("Parmetro no informado!");
        System.exit(-1);
    }
    System.out.println("Parmetro: " + args[0]);

    MongoClient mongoClient = new MongoClient();
    //MongoClient mongoClient = new MongoClient( "54.172.218.64" , 27017 );
    DB db = mongoClient.getDB("myDatabase");

    DBCollection collection = db.getCollection("ads");
    collection.drop();

    BulkWriteOperation builder = collection.initializeUnorderedBulkOperation();

    FileInputStream fileInputStream = new FileInputStream(".\\resources\\MongoDB" + args[0] + ".txt");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
    // Insert
    // Time start    
    long start = System.currentTimeMillis();

    String line;
    while ((line = bufferedReader.readLine()) != null) {
        DBObject bson = (DBObject) JSON.parse(line);
        builder.insert(bson);
    }
    bufferedReader.close();
    builder.execute();
    //Time end
    long elapsed = System.currentTimeMillis() - start;
    System.out.println("[insert] Time elapsed: " + elapsed + " ms");

    // Update
    // Time start    
    start = System.currentTimeMillis();
    collection.updateMulti(new BasicDBObject(),
            new BasicDBObject("$set", new BasicDBObject().append("ano", 2006)));
    // Time end
    elapsed = System.currentTimeMillis() - start;
    System.out.println("[update] Time elapsed: " + elapsed + " ms");

    // Select
    // Time start    
    start = System.currentTimeMillis();
    BasicDBObject keys = new BasicDBObject();
    keys.put("_id", 1);
    keys.put("modeloCarro.marca", 1);
    keys.put("modeloCarro.nome", 1);
    keys.put("uf", 1);
    keys.put("placa_carro", 1);
    keys.put("qtd_portas", 1);
    keys.put("cambio", 1);
    keys.put("combustivel", 1);
    keys.put("cor", 1);
    keys.put("km", 1);
    keys.put("valor", 1);
    keys.put("detalhe", 1);
    BasicDBObject sort = new BasicDBObject("_id", 1);

    DBCursor cursor = collection.find(new BasicDBObject(), keys).sort(sort);
    while (cursor.hasNext()) {
        cursor.next();
    }
    // Time end
    elapsed = System.currentTimeMillis() - start;
    System.out.println("[select] Time elapsed: " + elapsed + " ms");

    // Delete
    // Time start    
    start = System.currentTimeMillis();
    collection.remove(new BasicDBObject());
    // Time end
    elapsed = System.currentTimeMillis() - start;
    System.out.println("[delete] Time elapsed: " + elapsed + " ms");
}

From source file:mongodb.querysix.java

public static void main(String[] args) throws UnknownHostException {
    //Connect to mongoDB
    MongoClient mongo = new MongoClient();
    //Connect to database and collection
    DB db = mongo.getDB("db");
    DBCollection partialCollection = db.getCollection("tags_import");
    //Create object that queries for MovieIDs that contain MST3k
    DBObject partial = new BasicDBObject("tag", "MST3K");
    //Use cursor to walk through collection one step at a time
    DBCursor cursor = partialCollection.find(partial);
    try {/*  w  ww .  j av a 2 s .com*/
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }
}

From source file:mongodb.Ratings_5.java

public static void main(String[] args) throws UnknownHostException {
    MongoClient mongo = new MongoClient();
    DB db = mongo.getDB("db");
    DBCollection Collection = db.getCollection("ratings");

    //Specifying Rating = 5 in BasicDBObject since we want to find out movie id's for which Rating = 3
    BasicDBObject object = new BasicDBObject("Rating", 3);

    //Creating List to find out distinct movie ids which have rating = 3
    List MovieId = Collection.distinct("MovieID", object);

    System.out.println("The Movie Id's with rating 3: \n");

    for (int i = 0; i < MovieId.size(); i++) {
        System.out.println(MovieId.get(i).toString());
    }//w ww .j  a v  a  2  s  .co  m

}

From source file:mongodb.sortDocument.java

public static void main(String[] args) {
    try {//from  ww w  .j  a v  a 2s.c om
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        sortDocument.sortWordsAscending(collection);
        sortDocument.sortWordsDesc(collection);
        sortDocument.sortWordsAscAndSize(collection);
    } catch (Exception e) {
        System.out.println(e);
    }
}