/** * Method implements aggregation framework. * * @param firstOp requisite first operation to be performed in the aggregation pipeline * @param additionalOps additional operations to be performed in the aggregation pipeline * @return the aggregation operation's result set * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation * @mongodb.server.release 2.2 * @deprecated Use {@link #aggregate(List, AggregationOptions)} instead */ @Deprecated @SuppressWarnings("unchecked") public AggregationOutput aggregate(final DBObject firstOp, final DBObject... additionalOps) { List<DBObject> pipeline = new ArrayList<DBObject>(); pipeline.add(firstOp); Collections.addAll(pipeline, additionalOps); return aggregate(pipeline); }
/** * Method implements aggregation framework. * * @param pipeline operations to be performed in the aggregation pipeline * @param options options to apply to the aggregation * @return the aggregation operation's result set * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation * @mongodb.server.release 2.2 */ public Cursor aggregate(final List<? extends DBObject> pipeline, final AggregationOptions options) { return aggregate(pipeline, options, getReadPreference()); }
/** * Method implements aggregation framework. * * @param pipeline operations to be performed in the aggregation pipeline * @return the aggregation's result set * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation * @mongodb.server.release 2.2 * @deprecated Use {@link #aggregate(List, AggregationOptions)} instead */ @Deprecated public AggregationOutput aggregate(final List<? extends DBObject> pipeline) { return aggregate(pipeline, getReadPreference()); }
/** * Method implements aggregation framework. * * @param pipeline operations to be performed in the aggregation pipeline * @param options options to apply to the aggregation * @param readPreference {@link ReadPreference} to be used for this operation * @return the aggregation operation's result set * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation * @mongodb.server.release 2.2 */ public Cursor aggregate(final List<? extends DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference) { Cursor cursor = aggregate(pipeline, options, readPreference, true); if (cursor == null) { throw new MongoInternalException("cursor can not be null in this context"); } return cursor; }
@Override public <U> Iterator<U> aggregate(final String collectionName, final Class<U> target, final AggregationOptions options, final ReadPreference readPreference) { LOG.debug("stages = " + stages); Cursor cursor = collection.aggregate(stages, options, readPreference); return new MorphiaCursor<U>(datastore, cursor, mapper, target, mapper.createEntityCache()); }
/** * Method implements aggregation framework. * * @param pipeline operations to be performed in the aggregation pipeline * @param readPreference the read preference specifying where to run the query * @return the aggregation's result set * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation * @mongodb.server.release 2.2 * @deprecated Use {@link #aggregate(List, AggregationOptions, ReadPreference)} )} instead */ @Deprecated @SuppressWarnings("unchecked") public AggregationOutput aggregate(final List<? extends DBObject> pipeline, final ReadPreference readPreference) { Cursor cursor = aggregate(pipeline, AggregationOptions.builder().build(), readPreference, false); if (cursor == null) { return new AggregationOutput(Collections.<DBObject>emptyList()); } else { List<DBObject> results = new ArrayList<DBObject>(); while (cursor.hasNext()) { results.add(cursor.next()); } return new AggregationOutput(results); } }
/** * MongoDB aggregation. * * @param pipeline * @return */ public AggregationOutput aggregate(List<DBObject> pipeline) { return getFilesCollection().aggregate(pipeline); }
AggregationOutput output = mongoDb.getCollection(collectionName).aggregate(pipeline); aggregationResults = output.results();
BasicDBObject match = new BasicDBObject( "$match", new BasicDBObject("status", 1) ); BasicDBObject group = new BasicDBObject( "$group", new BasicDBObject("_id", null).append( "total", new BasicDBObject( "$sum", "$key1" ) ) ); DBCollection collection = db.getCollection("mycollection"); AggregationOutput output = collection.aggregate(match,group);
DBCollection colllection = mongoTemplate.getCollection("collectionName"); List<DBObject> pipeline = Arrays.<DBObject>asList( new BasicDBObject("$match",new BasicDBObject("dataset","d1")), new BasicDBObject("$group", new BasicDBObject("_id", new BasicDBObject("source","$source") .append("name","$name") .append("description","$description") ) ) ); AggregationOutput output = colllection.aggregate(pipeline);
DBCollection colllection = mongoTemplate.getCollection("collectionName"); List<DBObject> pipeline = Arrays.<DBObject>asList( new BasicDBObject("$match",new BasicDBObject("dataset","d1")), new BasicDBObject("$group", new BasicDBObject("_id","$source") .append("name",new BasicDBObject("$first","$name")) .append("description", new BasicDBObject("$first","$description")) ) ); AggregationOutput output = colllection.aggregate(pipeline);
DBCollection myColl = db.getCollection("keyword_db"); // for the $group operator DBObject groupFields = new BasicDBObject( "_id", "$names"); groupFields.put("count", new BasicDBObject( "$sum", 1)); DBObject group = new BasicDBObject("$group", groupFields ); // for the $sort operator DBObject sortFields = new BasicDBObject("count", -1); DBObject sort = new BasicDBObject("$sort", sortFields ); // run the aggregation AggregationOutput output = myColl.aggregate(group, sort); System.out.println( output.getCommandResult() );
// set up pipeline List<DBObject> ops = new ArrayList<DBObject>(); ops.add(new BasicDBObject("$out", "target")); // writes to collection "target" // run it MongoClient client = new MongoClient("host"); DBCollection source = client.getDB("db").getCollection("source") source.aggregate(ops);
public AggregationOutput aggregate(final DBObject... ops) { this.log("aggregate", ops); return MongoProxyConfig.this.collection.aggregate(Arrays.asList(ops)); } }
/** * Method implements aggregation framework. * * @param pipeline operations to be performed in the aggregation pipeline * @param options options to apply to the aggregation * @return the aggregation operation's result set * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation * @mongodb.server.release 2.2 */ public Cursor aggregate(final List<? extends DBObject> pipeline, final AggregationOptions options) { return aggregate(pipeline, options, getReadPreference()); }
/** * Method implements aggregation framework. * * @param pipeline operations to be performed in the aggregation pipeline * @return the aggregation's result set * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation * @mongodb.server.release 2.2 * @deprecated Use {@link #aggregate(List, AggregationOptions)} instead */ @Deprecated public AggregationOutput aggregate(final List<? extends DBObject> pipeline) { return aggregate(pipeline, getReadPreference()); }
DBCollection collection = db.getCollection("collectionName"); DBObject groupFieldsInId = new BasicDBObject(); groupFieldsInId.put( "field1", "$field1"); groupFieldsInId.put( "field2", "$field2"); DBObject groupFields = new BasicDBObject( "_id", groupFieldsInId); groupFields.put("count", new BasicDBObject( "$sum", 1)); DBObject group = new BasicDBObject("$group", groupFields ); AggregationOutput output = collection.aggregate(group);
final MongoClient mongoClient = new MongoClient(); final DB db = mongoClient.getDB("DB_NAME"); final DBCollection collection = db.getCollection("item"); final DBObject groupIdFields = new BasicDBObject("_id", "$name"); groupIdFields.put("count", new BasicDBObject("$sum", 1)); final DBObject group = new BasicDBObject("$group", groupIdFields); final DBObject projectFields = new BasicDBObject("_id", 0); projectFields.put("name", "$_id"); projectFields.put("count", 1); final DBObject project = new BasicDBObject("$project", projectFields); final AggregationOutput aggregate = collection.aggregate(group, project);
public Iterator<BasicDBObject> aggregate(String description, List<DBObject> pipeline) { if (log.isDebugEnabled()) { log.debug("--"+dbCollection.getName()+"-> "+description+" q="+toString(pipeline)); } AggregationOutput aggregationOutput = dbCollection.aggregate(pipeline); return new LoggingIterator(this, aggregationOutput.results().iterator()); }
@Override public <U> Iterator<U> aggregate(final String collectionName, final Class<U> target, final AggregationOptions options, final ReadPreference readPreference) { LOG.debug("stages = " + stages); Cursor cursor = collection.aggregate(stages, options, readPreference); return new MorphiaIterator<U, U>(datastore, cursor, mapper, target, collectionName, mapper.createEntityCache()); }