Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

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

Prototype

public MongoClient() 

Source Link

Document

Creates an instance based on a (single) mongodb node (localhost, default port).

Usage

From source file:com.avbravo.myapp.provider.MongoClientProvider.java

public MongoClient getMongoClient() {
    mongoClient = new MongoClient();
    try {//from  w  w  w  . j  ava2  s  .  c  o  m
        /**
         * autentificacion
         */
        /*
         String database = "";
        String username = "";
        String password = "";
        String host = "localhost";
        int port = 27017;
        char[] charArray = password.toCharArray();
        MongoCredential credential = MongoCredential.createCredential(username, database, charArray);
        mongoClient = new MongoClient(new ServerAddress(host, port), Arrays.asList(credential));
         */
    } catch (Exception e) {
        System.out.println("getMongoClient() " + e.getLocalizedMessage());
    }
    return mongoClient;
}

From source file:com.avbravo.myappweb.provider.MongoClientProvider.java

@PostConstruct
public void init() {
    try {/* w  w  w .  j ava 2s  .c  om*/

        /*
        String database="";
        String username="";
        String password="";
        String host="localhost";
        String port="27017";
        char[] charArray = password.toCharArray();
        MongoCredential credential = MongoCredential.createCredential(username, database, charArray);
        mongoClient = new MongoClient(new ServerAddress(host,port), Arrays.asList(credential));
         */
        mongoClient = new MongoClient();
    } catch (Exception e) {
        JsfUtil.addErrorMessage("init() " + e.getLocalizedMessage());
    }

}

From source file:com.averageloser.mongodemo.Model.DBHelper.java

public MongoClient getMongoClient() {
    if (client == null) {
        return client = new MongoClient();
    }

    return client;
}

From source file:com.ayu.filter.DbListner.java

License:Open Source License

/**
  * @see ServletContextListener#contextInitialized(ServletContextEvent)
  *///w w w .  j a va 2s .c o  m
public void contextInitialized(ServletContextEvent arg0) {
    MongoClient mongoClient = null;
    try {
        mongoClient = new MongoClient();
    } catch (UnknownHostException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    DB db = mongoClient.getDB("test");
    DBCollection coll;
    coll = db.getCollection("test");
    DBCursor cursor = coll.find();
    while (cursor.hasNext()) {
        String ip = (String) cursor.next().get("Ip-Address");
        if (arg0.getServletContext().getAttribute(ip) == null) {
            arg0.getServletContext().setAttribute(ip, ip);
        }
    }

}

From source file:com.ayu.filter.RegularService.java

License:Open Source License

@Async
public void registerUser(String ip, String date, String type, String document) {

    //System.out.println(" Attack from  "+ip +" captured at"+ date+" "+"type of attack is"+" "+type);
    //System.out.println(" Database Insertion ");

    try {//from   www .  ja v a2  s. co m
        Thread.sleep(5000);

    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    MongoClient mongoClient = null;
    try {
        mongoClient = new MongoClient();
    } catch (UnknownHostException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // or, to connect to a replica set, supply a seed list of members
    //MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),
    //                                    new ServerAddress("localhost", 27018),
    //                                  new ServerAddress("localhost", 27019)));

    DB db = mongoClient.getDB(document);
    DBCollection coll;
    coll = db.getCollection(document);
    BasicDBObject doc = new BasicDBObject("Ip-Address", ip).append("Date", date).append("Attack-Type", type);

    //System.out.println("Data Display");
    coll.insert(doc);
    mongoClient.close();

    //System.out.println(" Asynchronous method call of database  Complete ");

}

From source file:com.bcknds.demo.mongo.config.MongoConfig.java

License:Apache License

@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
    return new SimpleMongoDbFactory(new MongoClient(), database);
}

From source file:com.bryanreinero.purchase.Example.java

License:Open Source License

public static void main(String[] args) {
    try {/* w  w w. j a v  a2  s.  c om*/
        PurchaseDAO dao = new PurchaseDAO(new MongoClient());
        Random rand = new Random();

        Purchase purchase = new Purchase(new Integer(rand.nextInt(1000)), "buyer1", "seller1", 100,
                State.initial);
        // Insert new purchase transaction
        dao.insertTransaction(purchase);

        Transaction transaction = dao.getTransactionsInProgress();
        while (transaction != null) {
            transaction.transact(dao);
            transaction = dao.getTransactionsInProgress();
        }

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

From source file:com.chadcover.Homework_23.java

License:Apache License

public static void main(String[] args) {
    MongoClient client = new MongoClient();
    MongoDatabase database = client.getDatabase("students");
    MongoCollection<Document> collection = database.getCollection("gradesBak");

    long count = collection.count();
    System.out.println(count);/*from   www  .  j av  a  2 s. c om*/

    Bson filter = eq("type", "homework");
    Bson sort = ascending("student_id", "score");
    Bson projection = fields(include("student_id", "score"));

    // better for large datasets
    MongoCursor<Document> result = collection.find(filter).sort(sort).projection(projection).iterator();
    try {
        int lastId = 0;
        int counter = 0;
        while (result.hasNext()) {
            Document cur = result.next();
            int id = (Integer) cur.get("student_id");
            // when moving to new student
            // delete that record, which is the lowest homework score
            if (id != lastId || counter == 0) {
                double grade = (Double) cur.get("score");
                System.out.println("Delete this score: " + grade);
                collection.deleteOne(eq("_id", cur.get("_id")));
            } else {
                // do nothing
            }
            lastId = id;
            counter++;
        }
    } finally {
        result.close();
    }

}

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   w  w w  .j  a v a2s .  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 ww  . j  a va2  s  .  c o m
        public void apply(final Document document) {
            System.out.println(document);
            //System.out.println("hah");

        }
    });

}