List of usage examples for com.mongodb MongoClientURI MongoClientURI
public MongoClientURI(final String uri)
From source file:yelpapp.HW3.java
private static List proximityQuery(double longitude, double latitude, int miles) { List<String> result = new ArrayList<>(); MongoClient client = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); //MongoCollection<Document> db = client.getDatabase("YelpApplication").getCollection("YelpBusiness"); DB db1 = new MongoClient("localhost", 27017).getDB("YelpApplication"); //DBCollection dbcollection=db1.getCollection("YelpBusiness"); BasicDBObject q = new BasicDBObject(); DBCollection dbcollection = db1.getCollection("YelpBusiness"); BasicDBObject proj1 = new BasicDBObject(); proj1.put("business_id", 1); proj1.append("_id", 0); /* db.YelpBusiness.find( {/* ww w .j a v a 2 s. c o m*/ loc: { $near : { $geometry: { type: "Point", coordinates: [ -89.3083801269531, 43.1205749511719 ] }, $maxDistance: 80467.54 } } } ) { "loc" : { "$near" : { "$geometry" : { "type" : "Point" , "coordinates" : [ -89.3083801269531 , 43.1205749511719]}} , "$maxDistance" : 1609.34}} { "loc" : { "$near" : { "$geometry" : { "type" : "Point" , "coordinates" : [ -89.3083801269531 , 43.1205749511719]} , "$maxDistance" : 1609.34}}} */ if (miles < 0) miles = 1; BasicDBObject coordinates = new BasicDBObject("coordinates", asList(longitude, latitude)); BasicDBObject type = new BasicDBObject("type", "Point").append("coordinates", asList(longitude, latitude)); BasicDBObject geometry = new BasicDBObject("$geometry", type); System.out.println(geometry); double maxdistancekeyenin = 1609.34 * miles; BasicDBObject maxdistane = new BasicDBObject("$maxDistance", maxdistancekeyenin); BasicDBObject near = new BasicDBObject("$near", geometry.append("$maxDistance", maxdistancekeyenin)); BasicDBObject location = new BasicDBObject("loc", near); System.out.println(location); DBCursor resultset = dbcollection.find(location, proj1); while (resultset.hasNext()) { String str = (String) resultset.next().toString(); JSONParser parser = new JSONParser(); try { JSONObject jsonObject = (JSONObject) parser.parse(str.toString().trim()); String bid = (String) jsonObject.get("business_id"); if (isdebug) System.out.println(bid); result.add(bid); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /*MongoCollection<Document> b_collection=client.getDatabase("YelpApplication").getCollection("YelpBusinessAttributes"); FindIterable<Document> result = b_collection.find(new Document("categories",new Document(option, str)).append("attribute_keys", new Document(option, str2))).projection(fields(include("name", "city", "stars", "state"), excludeId())); List<String> businesses = new ArrayList<String>(); result.forEach(new Block<Document> (){ @Override public void apply(final Document document) { // TODO Auto-generated method stub businesses.add(document.toJson().toString().trim()); System.out.println(document.toJson().toString().trim()); } });*/ System.out.println("Done executing proximity"); return result; }
From source file:yelpapp.HW3.java
public List getReviewBusiness(Date reviewfrom, Date Reviewto, String Stars_cond, String Stars_value, String votes_cond, String votes_value) { MongoClient client;/* w ww . j a va 2 s . co m*/ client = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); DB db1 = new MongoClient("localhost", 27017).getDB("YelpApplication"); DBCollection dbcollection = db1.getCollection("YelpReview"); ArrayList businessiidList = null; String searchForComboBox = "and"; BasicDBObject votescountcondDBOB = null; BasicDBObject votescountcond1 = null; BasicDBObject averagestarscondDBOB = null; BasicDBObject averagestarscond1 = null; BasicDBObject begincondDBOB = null; BasicDBObject rdatecondDBOB = null; if (reviewfrom != null && Reviewto != null && (!Stars_value.equalsIgnoreCase("0") && !votes_value.equalsIgnoreCase("0"))) { DateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); String date_string_reviewfrom = df.format(reviewfrom); String date_string_reviewto = df.format(Reviewto); String rdatecond = "$gt"; begincondDBOB = new BasicDBObject("$gt", reviewfrom).append("$lt", Reviewto);//" ISODate("+datestr+")" rdatecondDBOB = new BasicDBObject("date", begincondDBOB); System.out.println("rdatecondDBOB" + rdatecondDBOB); if (!votes_value.equalsIgnoreCase("0")) { String reviewcond = condstr(votes_cond); votescountcondDBOB = new BasicDBObject(reviewcond, Double.parseDouble(votes_value)); votescountcond1 = new BasicDBObject("totalVotes", votescountcondDBOB); } if (!Stars_value.equalsIgnoreCase("0")) { String starscond = condstr(Stars_cond); averagestarscondDBOB = new BasicDBObject(starscond, Integer.parseInt(Stars_value)); averagestarscond1 = new BasicDBObject("stars", averagestarscondDBOB); } String datestr = "2014-12-09T00:00:00Z"; System.out.println("start" + "new ISODate(" + datestr + ")"); String option = "$and"; BasicDBObject proj1 = new BasicDBObject(); List<BasicDBObject> asList = new ArrayList<BasicDBObject>(); // asList.add(rdatecondDBOB);//reviewcountcond1,averagestarscond1,friendsDOB if (null != averagestarscond1) asList.add(averagestarscond1); if (null != rdatecondDBOB) asList.add(rdatecondDBOB); if (null != votescountcond1) asList.add(votescountcond1); DBObject optioncond = new BasicDBObject(option, asList); /* db.YelpUser.aggregate([ { $match: { $and: [ {date: {$gt: 3,$lt :5}}, {stars: {$gt: 3}}, {$votes.useful: {$gt:2}}, ] } } ])*/ System.out.println("optioncond =" + optioncond); System.out.println("option" + option); DBObject match = new BasicDBObject("$match", optioncond); System.out.println("match =" + match); DBObject project = new BasicDBObject("$project", new BasicDBObject("business_id", 1).append("_id", 0)); System.out.println(match); AggregationOutput output = dbcollection.aggregate(match, project); System.out.println("output" + output); // query = match.toString(); businessiidList = new ArrayList<String>(); for (DBObject result : output.results()) { System.out.println("reviews" + result); businessiidList.add(result.get("business_id")); //System.out.println(businessiidList); } } System.out.println("Done executing review"); return businessiidList; }