List of usage examples for com.mongodb.client MongoCollection aggregate
AggregateIterable<TDocument> aggregate(List<? extends Bson> pipeline);
From source file:com.mycompany.mongodb_exercise.MongoDBQueries.java
/** * Who are the most mentioned Twitter users? (Provide top five.) * * @return list of results//w ww.ja v a 2s . c o m */ public List<Object> getMostMentionedUsers() { MongoCollection<Document> mentionCollection = db.getCollection("most_mentioned"); AggregateIterable<Document> output = mentionCollection .aggregate( Arrays.asList(new Document("$sort", new Document("value", -1)), new Document("$limit", 5))) .allowDiskUse(Boolean.TRUE); List<Object> topFiveMentionedUsers = new ArrayList<>(); for (Document dbObject : output) { topFiveMentionedUsers.add(dbObject.get("_id") + ", " + dbObject.get("value")); } return topFiveMentionedUsers; }
From source file:connector.DBConnector.java
public static List<Object> tUsers() { MongoCollection<Document> coll = database.getCollection("tweets"); AggregateIterable<Document> output = coll.aggregate(Arrays.asList( new Document("$match", new Document("text", new Document("$regex", ".*@.*"))), new Document("$group", new Document("_id", new Document("user", "$user").append("tweet_id", "$id"))), new Document("$group", new Document("_id", "$_id.user").append("tweet_count", new Document("$sum", 1))), new Document("$project", new Document("_id", 0).append("user", "$_id").append("tweet_count", 1)), new Document("$sort", new Document("tweet_count", -1)), new Document("$limit", 10))) .allowDiskUse(Boolean.TRUE); List<Object> topUsers = new ArrayList<>(); for (Document dbObject : output) { topUsers.add(dbObject.get("user") + ", " + dbObject.get("tweet_count")); }//from w w w . j av a2 s . c om return topUsers; }
From source file:connector.DBConnector.java
public static List<Map<String, String>> mentionedTwitterUsers() { List<Map<String, Integer>> countList = new ArrayList(); List<Map<String, String>> resultList = new ArrayList(); MongoCollection<Document> coll = database.getCollection("tweets"); try (MongoCursor<BsonValue> cursor = coll.distinct("user", BsonValue.class).iterator()) { while (cursor.hasNext()) { String tempUser = cursor.next().toString().split("'")[1]; try (MongoCursor<Document> cursor2 = coll .aggregate(Arrays.asList(new BasicDBObject("$match", new BasicDBObject("text", new BasicDBObject("$regex", "@" + tempUser))))) .iterator()) {/*from ww w. j a v a2 s . co m*/ int count = 0; while (cursor2.hasNext()) { cursor2.next(); count++; } Map<String, Integer> m = new HashMap(); m.put(tempUser, count); countList.add(m); } } } catch (Exception e) { System.out.println(e.getMessage()); } countList.sort((Map<String, Integer> o1, Map<String, Integer> o2) -> { if (o1.values().iterator().next() < o2.values().iterator().next()) { return 1; } else { return -1; } }); for (int i = 0; i < 5; i++) { Map<String, String> m = new HashMap(); m.put(countList.get(i).keySet().iterator().next(), countList.get(i).values().iterator().next() + ""); resultList.add(m); } return resultList; }
From source file:connector.DBConnector.java
public static void mostActiveTwitterUsers() { MongoCollection<Document> coll = database.getCollection("tweets"); AggregateIterable<Document> output = coll.aggregate(Arrays.asList( new Document("$group", new Document("_id", "$user").append("count", new Document("$sum", 1))), new Document("$sort", new Document("count", -1)), new Document("$limit", 10))); for (Document dbObject : output) { System.out.println(dbObject); }//from w ww.j av a2 s.c o m }
From source file:connector.DBConnector.java
public static List<Object> mostGrumpy() { MongoCollection<Document> coll = database.getCollection("tweets"); List<Object> condArray = new ArrayList<>(); List<Object> eqArray = new ArrayList<>(); List<Object> divideArray = new ArrayList<>(); eqArray.add("$polarity"); eqArray.add(0);/*from w w w. j a v a2 s . c o m*/ divideArray.add("$tweet_count"); divideArray.add("$polarity"); condArray.add(new Document("$eq", eqArray)); condArray.add(0); condArray.add(new Document("$divide", divideArray)); AggregateIterable<Document> output = coll.aggregate(Arrays.asList( new Document("$group", new Document("_id", "$user").append("polarity", new Document("$sum", "$polarity")) .append("tweet_count", new Document("$sum", 1))), new Document("$project", new Document("_id", 0).append("user", "$_id") .append("avg_polarity", new Document("$cond", condArray)).append("polarity", 1) .append("tweet_count", 1)), new Document("$sort", new Document("avg_polarity", 1).append("tweet_count", -1)), new Document("$limit", 5))).allowDiskUse(Boolean.TRUE); List<Object> mostGrumpy = new ArrayList<>(); for (Document dbObject : output) { System.out.println(dbObject); mostGrumpy.add( dbObject.get("user") + ", " + dbObject.get("tweet_count") + ", " + dbObject.get("polarity")); } return mostGrumpy; }
From source file:connector.DBConnector.java
public static List<Object> mostHappy() { MongoCollection<Document> coll = database.getCollection("tweets"); List<Object> condArray = new ArrayList<>(); List<Object> eqArray = new ArrayList<>(); List<Object> divideArray = new ArrayList<>(); eqArray.add("$polarity"); eqArray.add(0);//from w w w . j av a 2 s. co m divideArray.add("$tweet_count"); divideArray.add("$polarity"); condArray.add(new Document("$eq", eqArray)); condArray.add(0); condArray.add(new Document("$divide", divideArray)); AggregateIterable<Document> output; output = coll.aggregate(Arrays.asList( new Document("$group", new Document("_id", "$user").append("polarity", new Document("$sum", "$polarity")) .append("tweet_count", new Document("$sum", 1))), new Document("$project", new Document("_id", 0).append("user", "$_id") // Results are added to the avg_polarity variable in the document. .append("avg_polarity", new Document("$cond", condArray)).append("polarity", 1) .append("tweet_count", 1)), new Document("$sort", new Document("avg_polarity", -1).append("tweet_count", -1)), new Document("$limit", 5))).allowDiskUse(Boolean.TRUE); List<Object> mostHappy = new ArrayList<>(); for (Document dbObject : output) { System.out.println(dbObject); mostHappy.add( dbObject.get("user") + ", " + dbObject.get("tweet_count") + ", " + dbObject.get("polarity")); } return mostHappy; }
From source file:consultasMongoDB.MongoConsultas.java
private static List<Object> retornaResultadoQueryComDoisParametros(String nomePrimeiroParametroDeProcura, String nomePrimeiroParametroDeProcura2) { MongoCollection<Document> ptCollection = initiateMongoCollection(); String regexNome = "^.*$"; AggregateIterable<Document> agg = ptCollection.aggregate(asList( new Document("$match", new Document("modulo", java.util.regex.Pattern.compile(regexNome))), new Document("$group", new Document("_id", new Document(nomePrimeiroParametroDeProcura, "$" + nomePrimeiroParametroDeProcura) .append(nomePrimeiroParametroDeProcura2, "$" + nomePrimeiroParametroDeProcura2)) .append("Total", new Document("$sum", "$area"))), new Document("$sort", new Document("Total", 1)))); List<Object> listO = new ArrayList<>(); agg.forEach(new Block<Document>() { @Override/* www. j a v a2s . c om*/ public void apply(final Document document) { int control = 1; //varivel para controlar a leitura (estava gravando dobrado) for (Object o : document.values()) { if (control == 1) { Document aux = (Document) document.get("_id"); String modAux = aux.getString(nomePrimeiroParametroDeProcura); String setAux = aux.getString(nomePrimeiroParametroDeProcura2); listO.add(modAux); listO.add(setAux); control = 2; } else { listO.add(document.getDouble("Total")); control = 1; } } } }); return listO; }
From source file:consultasMongoDB.MongoConsultas.java
private static List<Object> retornaResultadoQueryComTresParametros(String nomePrimeiroParametroDeProcura, String nomePrimeiroParametroDeProcura2, String nomePrimeiroParametroDeProcura3) { MongoCollection<Document> ptCollection = initiateMongoCollection(); String regexNome = "^.*$"; AggregateIterable<Document> agg = ptCollection.aggregate(asList( new Document("$match", new Document("modulo", java.util.regex.Pattern.compile(regexNome))), new Document("$group", new Document("_id", new Document(nomePrimeiroParametroDeProcura, "$" + nomePrimeiroParametroDeProcura) .append(nomePrimeiroParametroDeProcura2, "$" + nomePrimeiroParametroDeProcura2) .append(nomePrimeiroParametroDeProcura3, "$" + nomePrimeiroParametroDeProcura3)) .append("Total", new Document("$sum", "$area"))), new Document("$sort", new Document("Total", 1)))); List<Object> listO = new ArrayList<>(); agg.forEach(new Block<Document>() { @Override/*from w ww . ja v a 2s . c o m*/ public void apply(final Document document) { int control = 1; //varivel para controlar a leitura (estava gravando dobrado) for (Object o : document.values()) { if (control == 1) { Document aux = (Document) document.get("_id"); String modAux = aux.getString(nomePrimeiroParametroDeProcura); String setAux = aux.getString(nomePrimeiroParametroDeProcura2); String setAux2 = aux.getString(nomePrimeiroParametroDeProcura3); listO.add(modAux); listO.add(setAux); listO.add(setAux2); control = 2; } else { listO.add(document.getDouble("Total")); control = 1; } } } }); return listO; }
From source file:data.Project.java
License:Open Source License
public static ArrayList<Project> getProjectsByUserName(String loginName, DBManagerMongo manager) throws Exception { ArrayList<Project> list = new ArrayList<>(); MongoCollection<Document> coll = manager.getDb().getCollection("project"); Bson lookup = new Document("$lookup", new Document("from", "project_member").append("localField", "id") .append("foreignField", "project_id").append("as", "project_member")); Bson match = new Document("$match", new Document("project_member.user_login_name", loginName)); List<Bson> filters = new ArrayList<>(); filters.add(lookup);//from w w w .jav a 2 s . c o m filters.add(match); AggregateIterable<Document> it = coll.aggregate(filters); for (Document row : it) list.add(extractProject(row)); return list; }
From source file:data.ProjectPhase.java
License:Open Source License
public static ArrayList<String> getNamesByProjectName(String projectName, DBManagerMongo manager) throws Exception { ArrayList<String> list = new ArrayList<>(); MongoCollection<Document> coll = manager.getDb().getCollection("project_phase"); Bson lookup = new Document("$lookup", new Document("from", "project").append("localField", "project_id").append("foreignField", "id") //local field, remote field .append("as", "project")); Bson match = new Document("$match", new Document("project.name", projectName)); List<Bson> filters = new ArrayList<>(); filters.add(lookup);/* w w w . j a va 2 s. c o m*/ filters.add(match); AggregateIterable<Document> it = coll.aggregate(filters); for (Document row : it) list.add(row.getString("name")); return list; }