Example usage for com.mongodb MongoClient getDatabase

List of usage examples for com.mongodb MongoClient getDatabase

Introduction

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

Prototype

public MongoDatabase getDatabase(final String databaseName) 

Source Link

Usage

From source file:com.cognitive.cds.invocation.mongo.WorkProductSubscriptionDao.java

License:Apache License

public String deleteWorkProductSubscription(WorkProductSubscription wps) throws JsonProcessingException {

    mongoDbDao.setDatabase("work");
    MongoClient mongo = mongoDbDao.getMongoClient();
    MongoDatabase db = mongo.getDatabase("work");
    MongoCollection<Document> collection = db.getCollection("subscriptions");

    BasicDBObject query = new BasicDBObject();
    query.append("user", wps.getUser());

    DeleteResult result = collection.deleteOne(query);

    return result.toString();
}

From source file:com.company.Action.java

License:Apache License

public void run() throws NotFound, ResponseException, InterruptedException {
    Way2SMS way2SMS = new Way2SMS();
    way2SMS.Login(USERNAME, PASSWORD);//from   ww w  .ja  va  2 s .co m

    Set<User> hashset = new HashSet<>();
    Set<String> numbers = new HashSet<>();
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase database = mongoClient.getDatabase("Users");
    MongoCollection<Document> collection = database.getCollection("credenzappusers");
    //        System.out.println(collection.count());
    for (Document cur : collection.find()) {
        if (cur.get("Phone1") != null) {
            String value = String.valueOf(cur.get("Phone1"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name1")).split(" ")[0], value));
                }
            }
        }
        if (cur.get("Phone2") != null) {
            String value = String.valueOf(cur.get("Phone2"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name2")).split(" ")[0], value));
                }
            }
        }
        if (cur.get("Phone3") != null) {
            String value = String.valueOf(cur.get("Phone3"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name3")).split(" ")[0], value));
                }
            }
        }
        //            System.out.println("Phone2 is"+cur.get("Phone2"));
    }

    System.out.println(hashset.size());

    collection = database.getCollection("credenzreceipt");
    System.out.println(collection.count());
    for (Document cur : collection.find()) {
        if (cur.get("Contact1") != null) {
            String value = String.valueOf(cur.get("Contact1"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name1")).split(" ")[0], value));
                }
            }
        }
        if (cur.get("Contact2") != null) {
            String value = String.valueOf(cur.get("Contact2"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name2")).split(" ")[0], value));
                }
            }
        }
        if (cur.get("Contact3") != null) {
            String value = String.valueOf(cur.get("Contact3"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    try {
                        hashset.add(new User(((String) cur.get("Name3")).split(" ")[0], value));
                    } catch (Exception e) {
                        System.out.println("cought exception");
                    }
                }
            }
        }
        //            System.out.println("Phone2 is"+cur.get("Phone2"));
    }

    collection = database.getCollection("droidfest_users");
    //        System.out.println(collection.count());
    for (Document cur : collection.find()) {
        if (cur.get("phone_no") != null) {
            String value = String.valueOf(cur.get("phone_no"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("name")).split(" ")[0], value));
                }
            }
        }
    }

    collection = database.getCollection("hingemeetusers");
    //        System.out.println(collection.count());
    for (Document cur : collection.find()) {
        if (cur.get("Number") != null) {
            String value = String.valueOf(cur.get("Number"));
            if (value.length() == 10) {
                //                    System.out.println(value);
                if (!numbers.contains(value)) {
                    numbers.add(value);
                    hashset.add(new User(((String) cur.get("Name")).split(" ")[0], value));
                }
            }
        }
    }

    System.out.println(hashset.size());
    int i = 1;
    for (User user : hashset) {
        //            System.out.print(user.getName());
        //            System.out.println(user.getPhone_no());
        i++;
        if (i > 7) {
            way2SMS.SendSMS(user.getPhone_no(), "Hi " + user.getName()
                    + "! This is how you can build your Campus Startup: http://bit.ly/28Pa8aC FREE first lesson: http://bit.ly/28POhxJ Only for you:)");
            Thread.sleep(4000);
        } else {
            System.out.println("Interrupted for " + user.getPhone_no());
        }
        System.out.println(i);
    }
    //        List <User> number=new ArrayList<>();
    //        number.add(new User("lol","9819124829"));
    //        number.add(new User("lol","8446499908"));
    //        number.add(new User("lol","7709758284"));
    //        number.add(new User("lol","8956613425"));
    //        number.add(new User("lol","7276807536"));
    //        for (int i=0;i<number.size();i++) {
    //            way2SMS.SendSMS(number.get(i).getPhone_no(), "Hi "+number.get(i).getName()+"! This is how you can build your Campus Startup: http://bit.ly/28Pa8aC FREE first lesson: http://bit.ly/28POhxJ Only for you :)"); //args ; Phone no , Message
    //        }
}

From source file:com.DA.assignment1.Query.Demonstrate.java

public void Answer() throws Exception {
    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase("test");
    MongoCollection<Document> tag = db.getCollection("tags");
    MongoCollection<Document> movie = db.getCollection("movies");
    MongoCollection<Document> rating = db.getCollection("ratings");

    sum(movie);/*from  www . j a va2 s  .  com*/

    //selectMovie(rating);

    Tag(tag);

}

From source file:com.DA.assignment1.Query.Question1.java

public void Answer() {

    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase("test");

    FindIterable<Document> iterable1 = db.getCollection("movies").find(new Document("title", "Copycat (1995)"));
    iterable1.forEach(new Block<Document>() {
        @Override//from  w  w w.  ja va  2s . co m
        public void apply(final Document document) {
            System.out.println(document);
            //System.out.println("hah");

        }
    });

}

From source file:com.DA.assignment1.Query.Question2.java

public void Answer() {

    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase("test");

    AggregateIterable<Document> iterable = db.getCollection("movies")
            .aggregate(asList(new Document("$unwind", "$generes"),
                    new Document("$group",
                            new Document("_id", "$generes").append("count", new Document("$sum", 1))),
                    new Document("$sort", new Document("count", -1))));

    iterable.forEach(new Block<Document>() {
        @Override/* w ww  .j av  a  2 s  .  c om*/
        public void apply(final Document document) {
            System.out.println(document.toJson());
        }
    });

}

From source file:com.demo.mongodb.MongoDBSparkFreemarkerStyle.java

License:Apache License

public static void main(String[] args) {
    final Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(MongoDBSparkFreemarkerStyle.class, "/");

    MongoClient client = new MongoClient();

    MongoDatabase database = client.getDatabase("m101");
    final MongoCollection<Document> collection = database.getCollection("funnynumbers");

    Spark.get(new Route("/") {
        @Override/*w ww .  j a  v  a  2  s  .c om*/
        public Object handle(final Request request, final Response response) {
            StringWriter writer = new StringWriter();
            try {
                Template template = configuration.getTemplate("answer.ftl");

                // Not necessary yet to understand this.  It's just to prove that you
                // are able to run a command on a mongod server
                List<Document> results = collection.aggregate(asList(
                        new Document("$group",
                                new Document("_id", "$value").append("count", new Document("$sum", 1))),
                        new Document("$match", new Document("count", new Document("$lte", 2))),
                        new Document("$sort", new Document("_id", 1)))).into(new ArrayList<Document>());

                int answer = 0;
                for (Document cur : results) {
                    answer += (Double) cur.get("_id");
                }

                Map<String, String> answerMap = new HashMap<String, String>();
                answerMap.put("answer", Integer.toString(answer));

                template.process(answerMap, writer);
            } catch (Exception e) {
                e.printStackTrace();
                halt(500);
            }
            return writer;
        }
    });
}

From source file:com.dilmus.dilshad.scabi.ms.MetaServer.java

License:Open Source License

private static int createDatabasesIfAbsent(String dbHost, String dbPort) throws DScabiException {

    MongoClient mongo = new MongoClient(dbHost, Integer.parseInt(dbPort));
    MongoDatabase mongodb = mongo.getDatabase("MetaDB");
    MongoDatabase mongodb2 = mongo.getDatabase("AppTableDB");
    MongoDatabase mongodb3 = mongo.getDatabase("JavaFileDB");
    MongoDatabase mongodb4 = mongo.getDatabase("FileDB");
    log.debug("mongodb.getName() : {}", mongodb.getName());
    log.debug("mongodb2.getName() : {}", mongodb2.getName());
    log.debug("mongodb3.getName() : {}", mongodb3.getName());
    log.debug("mongodb4.getName() : {}", mongodb4.getName());

    mongo.close();//w w w .j  a va 2 s .  c om
    return 0;
}

From source file:com.enlightendev.mongodb.MongoDBSparkFreemarkerStyle.java

License:Apache License

public static void main(String[] args) {
    final Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(MongoDBSparkFreemarkerStyle.class, "/freemarker");

    MongoClient client = new MongoClient();

    MongoDatabase database = client.getDatabase("m101");
    final MongoCollection<Document> collection = database.getCollection("funnynumbers");

    Spark.get(new Route("/") {
        @Override/* w  w  w. j  a  v  a  2s  . c om*/
        public Object handle(final Request request, final Response response) {
            StringWriter writer = new StringWriter();
            try {
                Template template = configuration.getTemplate("answer.ftl");

                // Not necessary yet to understand this.  It's just to prove that you
                // are able to run a command on a mongod server
                List<Document> results = collection.aggregate(asList(
                        new Document("$group",
                                new Document("_id", "$value").append("count", new Document("$sum", 1))),
                        new Document("$match", new Document("count", new Document("$lte", 2))),
                        new Document("$sort", new Document("_id", 1)))).into(new ArrayList<Document>());

                int answer = 0;
                for (Document cur : results) {
                    answer += (Double) cur.get("_id");
                }

                Map<String, String> answerMap = new HashMap<String, String>();
                answerMap.put("answer", Integer.toString(answer));

                template.process(answerMap, writer);
            } catch (Exception e) {
                e.printStackTrace();
                halt(500);
            }
            return writer;
        }
    });
}

From source file:com.epam.dlab.mongo.MongoService.java

License:Apache License

public MongoService(MongoClient client, String databaseName) {
    this.client = client;
    this.databaseName = databaseName;
    this.database = client.getDatabase(databaseName);
}

From source file:com.exorath.service.connector.service.MongoDatabaseProvider.java

License:Apache License

public MongoDatabaseProvider(MongoClient client, String databaseName, String collectionName) {
    morphia.map(Server.class);
    datastore = (AdvancedDatastore) morphia.createDatastore(client, databaseName);
    this.collectionName = collectionName;
    try {//from w  ww .j ava 2s.c o  m
        client.getDatabase(databaseName).createCollection(collectionName);
    } catch (MongoCommandException e) {
        if (e.getErrorCode() != 48)
            throw e;
    }
}