/** * The implementation uses the MongoDB aggregation framework support Spring Data provides as well as SpEL expressions * to define arithmetical expressions. Note how we work with property names only and don't have to mitigate the nested * {@code $_id} fields MongoDB usually requires. * * @see example.springdata.mongodb.aggregation.OrderRepositoryCustom#getInvoiceFor(example.springdata.mongodb.aggregation.Order) */ @Override public Invoice getInvoiceFor(Order order) { AggregationResults<Invoice> results = operations.aggregate(newAggregation(Order.class, // match(where("id").is(order.getId())), // unwind("items"), // project("id", "customerId", "items") // .andExpression("'$items.price' * '$items.quantity'").as("lineTotal"), // group("id") // .sum("lineTotal").as("netAmount") // .addToSet("items").as("items"), // project("id", "items", "netAmount") // .and("orderId").previousOperation() // .andExpression("netAmount * [0]", taxRate).as("taxAmount") // .andExpression("netAmount * (1 + [0])", taxRate).as("totalAmount") // ), Invoice.class); return results.getUniqueMappedResult(); } }
private Long countLatestLaunches(Queryable filter) { Long total = 0L; final String countKey = "count"; List<AggregationOperation> operations = latestLaunchesAggregationOperationsList(filter); operations.add(Aggregation.count().as(countKey)); Map result = mongoTemplate.aggregate(newAggregation(operations), Launch.class, Map.class).getUniqueMappedResult(); if (null != result && result.containsKey(countKey)) { total = Long.valueOf(result.get(countKey).toString()); } return total; }
@Override public long getPageNumber(String entityId, Queryable filterable, Pageable pageable) { ImmutableList.Builder<AggregationOperation> pipelineBuilder = ImmutableList.<AggregationOperation>builder() .add(matchOperationFromFilter(filterable, mongoOperations, this.getEntityInformation().getJavaType())); if (null != pageable.getSort()){ pipelineBuilder.add( /* sort results as requested */ sort(pageable.getSort()) ); } pipelineBuilder.add( /* group items into one field pushing all results into one array */ group("result").push("$_id").as("array"), /* unwind array adding index to each result */ unwind("array", "ind", false), /* find needed item. How we know its index in query result */ match(where("array").is(ObjectId.isValid(entityId) ? new ObjectId(entityId) : entityId)), /* grab index only */ project("ind")); /* find items matching an provided filter */ Aggregation a = Aggregation .newAggregation(toArray(pipelineBuilder.build(), AggregationOperation.class)); final AggregationResults<Map> aggregate = mongoOperations.aggregate( a, getEntityInformation().getCollectionName(), Map.class); if (!aggregate.getUniqueMappedResult().containsKey("ind")) { throw new ReportPortalException(ErrorType.INCORRECT_FILTER_PARAMETERS, "Unable to calculate page number. Check your input parameters"); } /* result returned as long. Calculate page number */ return (long) Math.ceil ((((Long) aggregate.getUniqueMappedResult().get("ind")).doubleValue() + 1d) / (double) pageable.getPageSize()); }
); return mongoOperations.aggregate(a, mongoOperations.getCollectionName(Project.class), UserRoleDetails.class).getUniqueMappedResult();