Example usage for com.mongodb MongoClient close

List of usage examples for com.mongodb MongoClient close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes all resources associated with this instance, in particular any open network connections.

Usage

From source file:yelpmongodb.hw4.java

private void queryBusinesses(double[] poi, int proximity, ArrayList<String> maincat, ArrayList<String> att) {
    MongoClient client = new MongoClient();
    DB db = client.getDB("db");
    DBCollection coll = db.getCollection("business");
    String[] arr = { "$longitude", "$latitude" };
    List<DBObject> aggregate = new ArrayList<>();

    DBObject project = BasicDBObjectBuilder.start().push("$project").add("loc", arr)
            .add("business_id", "$business_id").add("name", "$name").add("city", "$city").add("state", "$state")
            .add("stars", "$stars").add("attributes", "$attributes").add("categories", "$categories").get();
    DBObject geoMatch = getGeoQuery(poi, proximity, 3963.2);
    DBObject mainMatch = mainCategoriesQuery(maincat, 1);
    DBObject attMatch = attributesQuery(att);
    aggregate.add(project);/*from  w w  w  . ja  va 2s .c  om*/
    aggregate.add(geoMatch);
    aggregate.add(mainMatch);
    aggregate.add(attMatch);
    AggregationOutput aggrcurs = coll.aggregate(aggregate);
    Iterable<DBObject> res = aggrcurs.results();
    Iterator iter = res.iterator();
    DefaultTableModel table = (DefaultTableModel) businessesTable.getModel();

    while (iter.hasNext()) {
        DBObject dbo = (DBObject) iter.next();
        table.addRow(
                new Object[] { dbo.get("name"), dbo.get("city"), dbo.get("state"), (double) dbo.get("stars") });
    }

    client.close();
}

From source file:yelpmongodb.hw4.java

private String getBusinessID(String name, String city, String state, double rating) {
    MongoClient client = new MongoClient();
    DB db = client.getDB("db");
    DBCollection coll = db.getCollection("business");
    BasicDBList businessID = new BasicDBList();
    DBObject bclause = new BasicDBObject("name", name);
    DBObject bclause2 = new BasicDBObject("city", city);
    DBObject bclause3 = new BasicDBObject("state", state);
    DBObject bclause4 = new BasicDBObject("stars", rating);
    businessID.add(bclause);//from  w w w .  j  a v a  2s . co m
    businessID.add(bclause2);
    businessID.add(bclause3);
    businessID.add(bclause4);
    DBObject businessQuery = new BasicDBObject("$and", businessID);
    DBCursor cursor = coll.find(businessQuery);
    String bid = cursor.next().get("business_id").toString();
    client.close();

    return bid;
}

From source file:yelp_mongo.Dbconnection.java

public Connection getconnection() {

    Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
    mongoLogger.setLevel(Level.SEVERE);
    // To connect to mongodb server
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    MongoDatabase db = mongoClient.getDatabase("mydb");
    System.out.println("Connect to database successfully");
    //boolean auth = db.authenticate("","");
    //System.out.println("Authentication: "+auth);
    MongoCollection<Document> coll = db.getCollection("yelp");

    System.out.println("Collection yelp selected successfully");

    /*FindIterable<Document> cursor = coll.find();
                 /*from w ww  .  j ava  2s . com*/
         cursor.forEach(new Block<Document>() {
         @Override
         public void apply(final Document document) {
             System.out.println(document);
         }
     });*/
    BasicDBObject clause1 = new BasicDBObject("yelping_since", "2012-02");
    //clause1.put("yelping_since", "2012-02");

    BasicDBObject clause2 = new BasicDBObject("review_count", new BasicDBObject("$gt", 500));
    BasicDBObject clause3 = new BasicDBObject("fans", new BasicDBObject("$gt", 15));
    BasicDBObject clause4 = new BasicDBObject("average_stars", new BasicDBObject("$gt", 3));
    //BasicDBObject cl5 = new BasicDBObject("$exist",true);
    //BasicDBObject cl6 = new BasicDBObject("$where","this.friends.length>49");
    //String cl = cl5.toString();
    //BasicDBObject c = new BasicDBObject(cl,cl6);
    //BasicDBObject f = new BasicDBObject("friends",new BasicDBObject("$exist",true));
    BasicDBObject clause5 = new BasicDBObject("friends.50", new BasicDBObject("$exists", true));
    //BasicDBObject cl5 = new BasicDBObject("$where","this.friends.length>3");

    /*BasicDBList fl = new BasicDBList();
    fl.add(f);
    fl.add(cl6);*/

    //clause2.put("review_count",gtquery);
    BasicDBList or = new BasicDBList();

    or.add(clause1);
    or.add(clause2);
    or.add(clause3);
    or.add(clause4);
    or.add(clause5);
    //or.add(fl);

    //fields.put("name",1);
    //fields.put("average_stars",1);
    //fields.put("fans",1);
    BasicDBObject query = new BasicDBObject("$and", or);

    FindIterable<Document> cursor = coll.find(query);
    System.out.println(query);
    cursor.forEach(new Block<Document>() {
        @Override
        public void apply(final Document document) {
            System.out.println(document.get("user_id"));
            System.out.println(document.get("name"));
            System.out.println(document.get("fans"));
            System.out.println(document.get("average_stars"));
            System.out.println(document.get("friends"));

        }
    });

    mongoClient.close();
    System.out.println("Connection successfully closed");

    return null;

}