com.mongodb.client.AggregateIterable Java Examples

The following examples show how to use com.mongodb.client.AggregateIterable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AuthorServiceImpl.java    From biliob_backend with MIT License 7 votes vote down vote up
@Override
public ResponseEntity getTopAuthor() {
    Calendar c = Calendar.getInstance();
    c.setTimeZone(TimeZone.getTimeZone("CTT"));
    c.add(Calendar.HOUR, 7);
    Date cDate = c.getTime();
    AggregateIterable<Document> r = mongoClient.getDatabase("biliob").getCollection("author")
            .aggregate(Arrays.asList(sort(descending("cFans")), limit(2),
                    project(Projections.fields(Projections.excludeId(),
                            Projections.include("name", "face", "official"),
                            Projections.computed("data",
                                    new Document().append("$filter",
                                            new Document().append("input", "$data")
                                                    .append("as", "eachData")
                                                    .append("cond", new Document().append("$gt",
                                                            Arrays.asList("$$eachData.datetime",
                                                                    cDate)))))))));
    ArrayList<Document> result = new ArrayList<>(2);
    for (Document document : r) {
        result.add(document);
    }

    return ResponseEntity.ok(result);
}
 
Example #2
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 7 votes vote down vote up
/**
 * 根据统计字段计算统计结果(gte最小值)并排序
 *
 * @param collectionName 集合名
 * @param match          match条件
 * @param field          统计字段
 * @param minCount       最小值
 * @return
 */
public LinkedHashMap<String, Integer> sortMap(String collectionName, Document match, String field, int minCount) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group("$" + field, Accumulators.sum("_count", 1))
                    , match(new Document("_count", new Document("$gte", minCount)))
                    , sort(new Document("_count", -1))
            )
    );

    LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
    MongoCursor<Document> iterator = aggregate.iterator();
    while (iterator.hasNext()) {
        Document next = iterator.next();
        map.put(next.getString("_id"), next.getInteger("_count"));
    }
    return map;
}
 
Example #3
Source File: MongoToElasticProvider.java    From mongolastic with MIT License 6 votes vote down vote up
/**
 * Get the MongoDB cursor.
 */
private MongoCursor<Document> getCursor(int skip) {
    if (cursor == null && cursorId == 0) {
        Document query = Document.parse(config.getMongo().getQuery());
        List<Bson> pipes = new ArrayList<>(3);
        pipes.add(match(query));
        pipes.add(skip(skip));

        Optional.ofNullable(config.getMongo().getProject()).ifPresent(p -> pipes.add(project(Document.parse(p))));

        AggregateIterable<Document> aggregate = collection.aggregate(pipes)
                .allowDiskUse(true)
                .useCursor(true);

        cursor = aggregate.iterator();

        // TODO: Persist cursor ID somewhere to allow restarts.
        Optional.ofNullable(cursor.getServerCursor()).ifPresent(serverCursor -> cursorId = serverCursor.getId());
    } else if (cursor == null && cursorId != 0) {
        // TODO: Lookup cursor ID for resume.
        // Open existing cursor in case of restart??
    }

    return cursor;
}
 
Example #4
Source File: MongoDbIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static List<Document> buildAutoBuckets(MongoDatabase mongoDatabase, Read spec) {
  List<Document> splitKeys = new ArrayList<>();
  MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(spec.collection());
  BsonDocument bucketAutoConfig = new BsonDocument();
  bucketAutoConfig.put("groupBy", new BsonString("$_id"));
  // 10 is the default number of buckets
  bucketAutoConfig.put("buckets", new BsonInt32(spec.numSplits() > 0 ? spec.numSplits() : 10));
  BsonDocument bucketAuto = new BsonDocument("$bucketAuto", bucketAutoConfig);
  List<BsonDocument> aggregates = new ArrayList<>();
  aggregates.add(bucketAuto);
  AggregateIterable<Document> buckets = mongoCollection.aggregate(aggregates);

  for (Document bucket : buckets) {
    Document filter = new Document();
    filter.put("_id", ((Document) bucket.get("_id")).get("min"));
    splitKeys.add(filter);
  }

  return splitKeys;
}
 
Example #5
Source File: AuthorServiceImpl.java    From biliob_backend with MIT License 5 votes vote down vote up
@Override
public ResponseEntity getLatestTopAuthorData() {
    AggregateIterable<Document> r = mongoClient.getDatabase("biliob").getCollection("author")
            .aggregate(Arrays.asList(sort(descending("cFans")), limit(2),
                    project(Projections.fields(Projections.excludeId(),
                            Projections.include("name", "face", "official"),
                            Projections.computed("data",
                                    new Document("$slice", Arrays.asList("$data", 1)))))));
    ArrayList<Document> result = new ArrayList<>(2);
    for (Document document : r) {
        result.add(document);
    }
    return ResponseEntity.ok(result);
}
 
Example #6
Source File: AggregationPipelineImpl.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public <U> Iterator<U> aggregate(final String collectionName, final Class<U> target,
                                 final AggregationOptions options,
                                 final ReadPreference readPreference) {
    LOG.debug("stages = " + stages);


    AggregateIterable<U> cursor = collection.aggregate(stages, target);
    return cursor.iterator();
}
 
Example #7
Source File: AggregationOptions.java    From morphia with Apache License 2.0 5 votes vote down vote up
/**
 * Applies the configured options to the collection.
 *
 * @param documents  the stage documents
 * @param collection the collection to configure
 * @param resultType the result type
 * @param <T>        the collection type
 * @param <S>        the result type
 * @return the updated collection
 * @morphia.internal
 */
public <S, T> AggregateIterable<S> apply(final List<Document> documents, final MongoCollection<T> collection,
                                         final Class<S> resultType) {
    MongoCollection<T> bound = collection;
    if (readConcern != null) {
        bound = bound.withReadConcern(readConcern);
    }
    if (readPreference != null) {
        bound = bound.withReadPreference(readPreference);
    }
    AggregateIterable<S> aggregate = bound.aggregate(documents, resultType)
                                          .allowDiskUse(allowDiskUse)
                                          .bypassDocumentValidation(bypassDocumentValidation);
    if (batchSize != null) {
        aggregate.batchSize(batchSize);
    }
    if (collation != null) {
        aggregate.collation(collation);
    }
    if (maxTimeMS != null) {
        aggregate.maxTime(getMaxTime(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
    }
    if (hint != null) {
        aggregate.hint(hint);
    }

    return aggregate;
}
 
Example #8
Source File: MDbOperation.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
static AggregateIterable<Document> callAggregateCmd( MongoCollection<Document> mongoCollection,
          QueryProperties queryProps ) throws OdaException
  {
      if( ! queryProps.hasAggregateCommand() )
          return null;
      DBObject operationExprObj = queryProps.getOperationExprAsParsedObject( true );
      if( operationExprObj == null )
          return null;
      
      // convert user-specified operation expression to operation pipeline
List<Document> operationList = QueryProperties
		.getObjectsAsDocumentList( operationExprObj );
      //DBObject firstOp = QueryProperties.getFirstObjectSet( operationExprObj );
      if( operationList == null )
          return null;     // no valid DBObject operation

      //DBObject[] addlOps = QueryProperties.getSecondaryObjectSets( operationExprObj );

      // aggregation $limit and $skip operators applies to the number 
      // of documents in the *input* pipeline, and thus cannot be used to apply
      // the searchLimit and numSkipDocuments properties defined for data set
      
      // $match and $sort pipeline operators are built in an aggregate command

      // execute the aggregate command
      
      try
      {
          return mongoCollection.aggregate( operationList );
      }
      catch( RuntimeException ex )
      {
          OdaException odaEx = new OdaException( Messages.mDbOp_aggrCmdFailed );
          odaEx.initCause( ex );
          throw odaEx;
      }        
  }
 
Example #9
Source File: AggregatePrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Test
public void filterAndGroupDocuments() {

	// @begin: filter-and-group-documents
	// @code: start
	AggregateIterable<Document> iterable = db.getCollection("restaurants")
		.aggregate(asList(
			new Document("$match", new Document("borough", "Queens").append("cuisine", "Brazilian")),
			new Document("$group",
				new Document("_id", "$address.zipcode").append("count", new Document("$sum", 1)))));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document.toJson());
		}
	});
	// @code: end

	/*
	 * // @results: start { "_id" : "11377", "count" : 1 } { "_id" : "11368", "count"
	 * : 1 } { "_id" : "11101", "count" : 2 } { "_id" : "11106", "count" : 3 } { "_id"
	 * : "11103", "count" : 1 } // @results: end
	 */

	// @end: filter-and-group-documents
}
 
Example #10
Source File: AggregatePrimer.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Test
public void groupDocumentsByAFieldAndCalculateCount() {

	// @begin: group-documents-by-a-field-and-calculate-count
	// @code: start
	AggregateIterable<Document> iterable = db.getCollection("restaurants").aggregate(asList(
		new Document("$group", new Document("_id", "$borough").append("count", new Document("$sum", 1)))));
	// @code: end

	// @pre: Iterate the results and apply a block to each resulting document
	// @code: start
	iterable.forEach(new Block<Document>() {
		@Override
		public void apply(final Document document) {
			System.out.println(document.toJson());
		}
	});
	// @code: end

	/*
	 * // @results: start { "_id" : "Missing", "count" : 51 } { "_id" :
	 * "Staten Island", "count" : 969 } { "_id" : "Manhattan", "count" : 10259 } {
	 * "_id" : "Brooklyn", "count" : 6086 } { "_id" : "Queens", "count" : 5656 } {
	 * "_id" : "Bronx", "count" : 2338 } // @results: end
	 */

	// @end: group-documents-by-a-field-and-calculate-count
}
 
Example #11
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 统计值 是否在统计结果(gte最小值)中
 *
 * @param collectionName 集合名
 * @param match          match条件
 * @param field          统计字段
 * @param value          统计值
 * @param minCount       最小值
 * @return
 */
public boolean inSortMap(String collectionName, Document match, String field, Object value, int minCount) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match.append(field, value))
                    , group("$" + field, Accumulators.sum("_count", 1))
                    , match(new Document("_count", new Document("$gte", minCount)))
            )
    );

    Document first = aggregate.first();
    return first == null ? false : true;
}
 
Example #12
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 采样标准差统计
 *
 * @param collectionName
 * @param match
 * @param stdDevField
 * @param sampleSize
 * @return
 */
public Double stdDevSamp(String collectionName, Document match, String stdDevField, int sampleSize) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , sample(sampleSize)
                    , group(null, Accumulators.stdDevSamp("_stdDev", "$" + stdDevField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_stdDev");
    }
    return null;
}
 
Example #13
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 最近统计
 *
 * @param collectionName
 * @param match
 * @param lastField
 * @param sort
 * @return
 */
public Object last(String collectionName, Document match, String lastField, Document sort) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , sort(sort)
                    , group(null, Accumulators.last("_last", "$" + lastField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.get("_last");
    }
    return null;
}
 
Example #14
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Aggregates documents according to the specified aggregation pipeline.
 *
 * @param pipeline    the aggregation pipeline
 * @param resultClass the class to decode each document into
 * @param <ResultT>   the target document type of the iterable.
 * @return an iterable containing the result of the aggregation operation
 */
<ResultT> AggregateIterable<ResultT> aggregate(
    final MongoNamespace namespace,
    final List<? extends Bson> pipeline,
    final Class<ResultT> resultClass) {
  this.waitUntilInitialized();

  ongoingOperationsGroup.enter();
  try {
    return getLocalCollection(namespace).aggregate(pipeline, resultClass);
  } finally {
    ongoingOperationsGroup.exit();
  }
}
 
Example #15
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 统计频数
 *
 * @param collectionName
 * @param match
 * @param distinctField
 * @return
 */
public int distinctCount(String collectionName, Document match, String distinctField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, addToSet("_array", "$" + distinctField))
                    , project(new Document("_num", new Document("$size", "$_array")))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getInteger("_num");
    }
    return 0;
}
 
Example #16
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取不一样的记录
 *
 * @param collectionName
 * @param match
 * @param distinctField
 * @return
 */
public List distinct(String collectionName, Document match, String distinctField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, addToSet("_array", "$" + distinctField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return (List) first.get("_array");
    }
    return null;
}
 
Example #17
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 最大统计
 *
 * @param collectionName
 * @param match
 * @param maxField
 * @return
 */
public Object max(String collectionName, Document match, String maxField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.max("_max", "$" + maxField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.get("_max");
    }
    return null;
}
 
Example #18
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 最小统计
 *
 * @param collectionName
 * @param match
 * @param minField
 * @return
 */
public Object min(String collectionName, Document match, String minField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.min("_min", "$" + minField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.get("_min");
    }
    return null;
}
 
Example #19
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 合统计
 *
 * @param collectionName
 * @param match
 * @param sumField
 * @return
 */
public Double sum(String collectionName, Document match, String sumField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.sum("_sum", "$" + sumField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_sum");
    }
    return null;
}
 
Example #20
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 平均统计
 *
 * @param collectionName
 * @param match
 * @param avgField
 * @return
 */
public Double avg(String collectionName, Document match, String avgField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.avg("_avg", "$" + avgField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_avg");
    }
    return null;
}
 
Example #21
Source File: MongoDao.java    From Liudao with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 标准差统计
 *
 * @param collectionName
 * @param match
 * @param stdDevField
 * @return
 */
public Double stdDevPop(String collectionName, Document match, String stdDevField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.stdDevPop("_stdDev", "$" + stdDevField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_stdDev");
    }
    return null;
}
 
Example #22
Source File: QueryConverter.java    From sql-to-mongo-db-query-converter with Apache License 2.0 4 votes vote down vote up
/**
 * @param mongoDatabase the database to run the query against.
 * @param <T> variable based on the type of query run.
 * @return When query does a find will return QueryResultIterator&lt;{@link org.bson.Document}&gt;
 *           When query does a count will return a Long
 *           When query does a distinct will return QueryResultIterator&lt;{@link java.lang.String}&gt;
 * @throws ParseException when the sql query cannot be parsed
 */
@SuppressWarnings("unchecked")
public <T> T run(MongoDatabase mongoDatabase) throws ParseException {
    MongoDBQueryHolder mongoDBQueryHolder = getMongoQuery();

    MongoCollection mongoCollection = mongoDatabase.getCollection(mongoDBQueryHolder.getCollection());

    if (SQLCommandType.SELECT.equals(mongoDBQueryHolder.getSqlCommandType())) {
    	
        if (mongoDBQueryHolder.isDistinct()) {
            return (T) new QueryResultIterator<>(mongoCollection.distinct(getDistinctFieldName(mongoDBQueryHolder), mongoDBQueryHolder.getQuery(), String.class));
        } else if (sqlCommandInfoHolder.isCountAll() && !isAggregate(mongoDBQueryHolder)) {
            return (T) Long.valueOf(mongoCollection.count(mongoDBQueryHolder.getQuery()));
        } else if (isAggregate(mongoDBQueryHolder)) {
            
            AggregateIterable aggregate = mongoCollection.aggregate(generateAggSteps(mongoDBQueryHolder,sqlCommandInfoHolder));

            if (aggregationAllowDiskUse != null) {
                aggregate.allowDiskUse(aggregationAllowDiskUse);
            }

            if (aggregationBatchSize != null) {
                aggregate.batchSize(aggregationBatchSize);
            }

            return (T) new QueryResultIterator<>(aggregate);
        } else {
            FindIterable findIterable = mongoCollection.find(mongoDBQueryHolder.getQuery()).projection(mongoDBQueryHolder.getProjection());
            if (mongoDBQueryHolder.getSort() != null && mongoDBQueryHolder.getSort().size() > 0) {
                findIterable.sort(mongoDBQueryHolder.getSort());
            }
            if (mongoDBQueryHolder.getOffset() != -1) {
                findIterable.skip((int) mongoDBQueryHolder.getOffset());
            }
            if (mongoDBQueryHolder.getLimit() != -1) {
                findIterable.limit((int) mongoDBQueryHolder.getLimit());
            }

            return (T) new QueryResultIterator<>(findIterable);
        }
    } else if (SQLCommandType.DELETE.equals(mongoDBQueryHolder.getSqlCommandType())) {
        DeleteResult deleteResult = mongoCollection.deleteMany(mongoDBQueryHolder.getQuery());
        return (T)((Long)deleteResult.getDeletedCount());
    } else {
        throw new UnsupportedOperationException("SQL command type not supported");
    }
}
 
Example #23
Source File: NonReactiveMongoNativeJavaDriverQueryExecutor.java    From mongodb-aggregate-query-support with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked", "deprecation"})
@Override
public Object executeQuery(QueryProvider queryProvider) {
  List<String> queries = queryProvider.getPipelines();
  Iterator<String> iterator = queries.iterator();
  int i = 0;

  String collectionName = queryProvider.getCollectionName();
  List<Bson> pipelineStages = new ArrayList<>();
  while (iterator.hasNext()) {
    String query = iterator.next();
    LOGGER.trace("Processing query string {} for pipeline stage {}", query, i++);
    Bson document = BsonDocument.parse(query);
    pipelineStages.add(document);
  }
  // run the pipeline and return a flux.
  MongoCollection<Document> collection = mongoOperations.getCollection(collectionName);

  // execute the query
  AggregateIterable<Document> aggregateIterable = collection.aggregate(pipelineStages)
                                                            .allowDiskUse(queryProvider.isAllowDiskUse())
                                                            .maxTime(queryProvider.getMaxTimeMS(), MILLISECONDS);
  if (isMongo360OrLater) {
    // after 3.6 CURSOR MODE is mandatory
    aggregateIterable.useCursor(true);
    LOGGER.debug("Mongo 3.6 detected - will use cursor mode for aggregate output");
  }
  try (MongoCursor<Document> cursor = aggregateIterable.iterator()) {
    // e.g. a pipeline with an @Out stage would not have any return value.
    if (isVoidReturnType(queryProvider)) {
      return null;
    }
    Class outputClass = queryProvider.getOutputClass();
    if (cursor.hasNext()) {
      if (!queryProvider.isPageable() || (queryProvider.isPageable() &&
                                          List.class.isAssignableFrom(queryProvider.getMethodReturnType()))) {
        return getNonPageResults(queryProvider, cursor);
      }
      else if (queryProvider.isPageable() && isPageReturnType(queryProvider)) {
        return getPageableResults(queryProvider, cursor);
      }
    }
  }

  return null;
}
 
Example #24
Source File: MatchDao.java    From render with GNU General Public License v2.0 4 votes vote down vote up
private Set<String> getMultiConsensusGroupIds(final MatchCollectionId collectionId,
                                              final boolean includeQGroupIds)
        throws IllegalArgumentException {

    final MongoCollection<Document> matchCollection = getExistingCollection(collectionId);

    final List<Document> pipeline = new ArrayList<>();
    pipeline.add(new Document("$match",
                              new Document("consensusSetData",
                                           new Document(QueryOperators.EXISTS, true))));

    if (includeQGroupIds) {

        // db.<matchCollection>.aggregate(
        //     [
        //         { "$match": { "consensusSetData": { "$exists": true } } },
        //         { "$group": { "_id": { "pGroupId": "$pGroupId", "qGroupId": "$qGroupId" } } }
        //     ]
        // )

        pipeline.add(new Document("$group",
                                  new Document("_id",
                                               new Document("pGroupId", "$pGroupId").
                                                       append("qGroupId", "$qGroupId"))));
    } else {

        // db.<matchCollection>.aggregate(
        //     [
        //         { "$match": { "consensusSetData": { "$exists": true } } },
        //         { "$group": { "_id": { "pGroupId": "$pGroupId" } } }
        //     ]
        // )

        pipeline.add(new Document("$group",
                                  new Document("_id",
                                               new Document("pGroupId", "$pGroupId"))));
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("getMultiConsensusGroupIds: running {}.aggregate({})",
                  MongoUtil.fullName(matchCollection),
                  MongoUtil.toJson(pipeline));
    }

    // sort and reduce to distinct set of group ids here instead of in pipeline
    final TreeSet<String> groupIdsWithMultiplePairs = new TreeSet<>();

    // mongodb java 3.0 driver notes:
    // -- need to set cursor batchSize to prevent NPE from cursor creation
    final AggregateIterable<Document> iterable = matchCollection.aggregate(pipeline).batchSize(1);
    try (final MongoCursor<Document> cursor = iterable.iterator()) {
        while (cursor.hasNext()) {
            final Document id = cursor.next().get("_id", Document.class);
            groupIdsWithMultiplePairs.add(id.getString("pGroupId"));
            if (includeQGroupIds) {
                groupIdsWithMultiplePairs.add(id.getString("qGroupId"));
            }
        }
    }

    return groupIdsWithMultiplePairs;
}
 
Example #25
Source File: MDbOperation.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
   * Applies data set query properties and hints on DBCursor, except
   * for cursor limit.
   * @see #applyPropertiesToCursor(DBCursor,QueryProperties,boolean,boolean)
   */
  static void applyPropertiesToCursor( MongoIterable<Document> mongoIterable, QueryProperties queryProps, 
          boolean includeSortExpr )
  {
BasicDBObject sortExprObj = null;
      if( includeSortExpr )   // normally done only when executing a query to get full result set
      {
          
          try
          {
              sortExprObj = queryProps.getSortExprAsParsedObject();
          }
          catch( OdaException ex )
          {
              // log warning and ignore
              DriverUtil.getLogger().log( Level.WARNING, 
                      Messages.bind( "Unable to parse the user-defined Sort Expression: {0}", queryProps.getSortExpr() ),  //$NON-NLS-1$
                      ex );
          }
         
      }

// Map it to correct iterable object
FindIterable<Document> findIterable = null;
AggregateIterable<Document> aggregateIterable = null;
MapReduceIterable<Document> mapReduceIterable = null;
if ( mongoIterable instanceof FindIterable )
{
	findIterable = (FindIterable<Document>) mongoIterable;
}
else if ( mongoIterable instanceof AggregateIterable )
{
	aggregateIterable = (AggregateIterable<Document>) mongoIterable;
}
else if ( mongoIterable instanceof MapReduceIterable )
{
	mapReduceIterable = (MapReduceIterable<Document>) mongoIterable;
}
if ( findIterable == null
		&& aggregateIterable == null && mapReduceIterable == null )
{
	// Unknown type, return
}

if ( findIterable != null )
{
	if ( sortExprObj != null )
		findIterable.sort( sortExprObj );

	if ( queryProps.getBatchSize( ) > 0 )
		findIterable.batchSize( queryProps.getBatchSize( ) );

	if ( queryProps.getNumDocsToSkip( ) > 0 )
		findIterable.skip( queryProps.getNumDocsToSkip( ) );

	if ( queryProps.isPartialResultsOk( ) )
		findIterable.partial( true );
	// TODO: Remove hint from the UI
	// TODO: add Time out in the UI
	/*
	 * // hint is deprecated in 3.2 DBObject hintObj =
	 * queryProps.getIndexHintsAsParsedObject(); String hintValue =
	 * queryProps.getIndexHints(); if( hintObj != null )
	 * rowsCursor.hint( hintObj ); else // try to pass the hint string
	 * value as is { String hintValue = queryProps.getIndexHints(); if(
	 * ! hintValue.isEmpty() ) rowsCursor.hint( hintValue ); }
	 * findIterable.maxTime(Bytes.QUERYOPTION_NOTIMEOUT, arg1) if(
	 * queryProps.hasNoTimeOut() ) rowsCursor.addOption(
	 * Bytes.QUERYOPTION_NOTIMEOUT );
	 */
}
if ( aggregateIterable != null )
{
	if ( queryProps.getBatchSize( ) > 0 )
		aggregateIterable.batchSize( queryProps.getBatchSize( ) );

}
if ( mapReduceIterable != null )
{
	if ( sortExprObj != null )
		mapReduceIterable.sort( sortExprObj );

	if ( queryProps.getBatchSize( ) > 0 )
		mapReduceIterable.batchSize( queryProps.getBatchSize( ) );
}
  }
 
Example #26
Source File: MongoClientTemplet.java    From mongodb-orm with Apache License 2.0 4 votes vote down vote up
private <T> List<T> aggregate(String statement, Object parameter, ResultHandler handler, ReadPreference readPreference) {
  if (logger.isDebugEnabled()) {
    logger.debug("Execute 'aggregate' mongodb command. Statement '" + statement + "'.");
  }

  AggregateConfig config = (AggregateConfig) configuration.getStatement(statement);
  if (config == null) {
    throw new MongoDaoException(statement, "Aggregate statement id '" + statement + "' not found.");
  }

  String collection = config.getCollection();
  Map<String, NodeEntry> function = config.getFunction();
  NodeEntry field = config.getField();

  MongoDatabase db = getDatabase();
  MongoCollection<Document> coll = db.getCollection(collection).withReadPreference(readPreference);

  List<Document> operations = new ArrayList<Document>(function.size());
  for (Map.Entry<String, NodeEntry> entry : function.entrySet()) {
    NodeEntry ne = entry.getValue();
    Map<String, Object> op = (Map<String, Object>) ne.executorNode(config.getNamespace(), configuration, parameter);
    Document operation = new Document(op);
    operations.add(operation);
    if (logger.isDebugEnabled()) {
      logger.debug("Execute 'aggregate' mongodb command. Operation '" + operation + "'.");
    }
  }

  AggregateIterable<Document> iterable = coll.aggregate(operations);

  List<Document> list = new ArrayList<Document>();
  MongoCursor<Document> iterator = iterable.iterator();
  while (iterator.hasNext()) {
    list.add(iterator.next());
  }

  if (logger.isDebugEnabled()) {
    logger.debug("Execute 'aggregate' mongodb command. Result set '" + list + "'.");
  }

  if (handler != null) {
    handler.handleResult(new ResultContext() {
      @Override
      public Object getResultObject() {
        return list;
      }

      @Override
      public int getResultCount() {
        return list.size();
      }
    });
    return null;
  }

  List<T> result = new ArrayList<T>(list.size());
  for (Document doc : list) {
    T t = (T) helper.toResult(config.getNamespace(), field, doc);
    result.add(t);
  }
  return result;
}
 
Example #27
Source File: RunMongoAggregation.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = null;
    if (context.hasIncomingConnection()) {
        flowFile = session.get();

        if (flowFile == null && context.hasNonLoopConnection()) {
            return;
        }
    }

    final String query = context.getProperty(QUERY).evaluateAttributeExpressions(flowFile).getValue();
    final Boolean allowDiskUse = context.getProperty(ALLOW_DISK_USE).asBoolean();
    final String queryAttr = context.getProperty(QUERY_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
    final Integer batchSize = context.getProperty(BATCH_SIZE).asInteger();
    final Integer resultsPerFlowfile = context.getProperty(RESULTS_PER_FLOWFILE).asInteger();
    final String jsonTypeSetting = context.getProperty(JSON_TYPE).getValue();
    final String dateFormat      = context.getProperty(DATE_FORMAT).evaluateAttributeExpressions(flowFile).getValue();

    configureMapper(jsonTypeSetting, dateFormat);

    Map<String, String> attrs = new HashMap<>();
    if (queryAttr != null && queryAttr.trim().length() > 0) {
        attrs.put(queryAttr, query);
    }

    MongoCursor<Document> iter = null;

    try {
        MongoCollection<Document> collection = getCollection(context, flowFile);
        List<Bson> aggQuery = buildAggregationQuery(query);
        AggregateIterable<Document> it = collection.aggregate(aggQuery).allowDiskUse(allowDiskUse);;
        it.batchSize(batchSize != null ? batchSize : 1);

        iter = it.iterator();
        List<Document> batch = new ArrayList<>();
        Boolean doneSomething = false;

        while (iter.hasNext()) {
            batch.add(iter.next());
            if (batch.size() == resultsPerFlowfile) {
                writeBatch(buildBatch(batch), flowFile, context, session, attrs, REL_RESULTS);
                batch = new ArrayList<>();
                doneSomething |= true;
            }
        }

        if (! batch.isEmpty()) {
            // Something remains in batch list, write it to RESULT
            writeBatch(buildBatch(batch), flowFile, context, session, attrs, REL_RESULTS);
        } else if (! doneSomething) {
            // The batch list is empty and no batch was written (empty result!), so write empty string to RESULT
            writeBatch("", flowFile, context, session, attrs, REL_RESULTS);
        }

        if (flowFile != null) {
            session.transfer(flowFile, REL_ORIGINAL);
        }
    } catch (Exception e) {
        getLogger().error("Error running MongoDB aggregation query.", e);
        if (flowFile != null) {
            session.transfer(flowFile, REL_FAILURE);
        }
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
}
 
Example #28
Source File: PipelineResultIteration.java    From rya with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor.
 * @param aggIter Iterator of documents in AggregationPipelineQueryNode's
 *  intermediate solution representation.
 * @param varToOriginalName A mapping from field names in the pipeline
 *  result documents to equivalent variable names in the original query.
 *  Where an entry does not exist for a field, the field name and variable
 *  name are assumed to be the same.
 * @param bindings A partial solution. May be empty.
 */
public PipelineResultIteration(AggregateIterable<Document> aggIter,
        Map<String, String> varToOriginalName,
        BindingSet bindings) {
    this.varToOriginalName = Preconditions.checkNotNull(varToOriginalName);
    this.bindings = Preconditions.checkNotNull(bindings);
    Preconditions.checkNotNull(aggIter);
    aggIter.batchSize(BATCH_SIZE);
    this.cursor = aggIter.iterator();
}
 
Example #29
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 2 votes vote down vote up
/**
 * Aggregates documents according to the specified aggregation pipeline.
 *
 * @param pipeline the aggregation pipeline
 * @return an iterable containing the result of the aggregation operation
 */
AggregateIterable<BsonDocument> aggregate(
    final MongoNamespace namespace,
    final List<? extends Bson> pipeline) {
  return aggregate(namespace, pipeline, BsonDocument.class);
}
 
Example #30
Source File: IMongoSession.java    From ymate-platform-v2 with Apache License 2.0 votes vote down vote up
<T extends IEntity, RESULT> AggregateIterable<RESULT> aggregate(Class<T> entity, Class<RESULT> resultClass, Aggregation... aggregations) throws Exception;