Example usage for org.springframework.data.mongodb.core.aggregation Fields UNDERSCORE_ID_REF

List of usage examples for org.springframework.data.mongodb.core.aggregation Fields UNDERSCORE_ID_REF

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.aggregation Fields UNDERSCORE_ID_REF.

Prototype

String UNDERSCORE_ID_REF

To view the source code for org.springframework.data.mongodb.core.aggregation Fields UNDERSCORE_ID_REF.

Click Source Link

Usage

From source file:org.devgateway.ocds.web.rest.controller.CostEffectivenessVisualsController.java

@ApiOperation(value = "Cost effectiveness of Tenders:"
        + " Displays the total amount of the active tenders that have active awards, "
        + "grouped by year. Only tenders.status=active"
        + "are taken into account. The year is calculated from tenderPeriod.startDate")
@RequestMapping(value = "/api/costEffectivenessTenderAmount", method = { RequestMethod.POST,
        RequestMethod.GET }, produces = "application/json")
public List<DBObject> costEffectivenessTenderAmount(
        @ModelAttribute @Valid final GroupingFilterPagingRequest filter) {

    DBObject project = new BasicDBObject();
    project.put("year", new BasicDBObject("$year", ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE)));
    addYearlyMonthlyProjection(filter, project, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
    project.put(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT, 1);
    project.put(Fields.UNDERSCORE_ID, "$tender._id");
    project.put("tenderWithAwards", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq",
            Arrays.asList(ref(MongoConstants.FieldNames.AWARDS_STATUS), Award.Status.active.toString())), 1,
            0)));//from  w  w  w.j a v a  2 s  .co  m
    project.put("tenderWithAwardsValue",
            new BasicDBObject("$cond",
                    Arrays.asList(
                            new BasicDBObject("$eq",
                                    Arrays.asList(ref(MongoConstants.FieldNames.AWARDS_STATUS),
                                            Award.Status.active.toString())),
                            ref(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT), 0)));
    project.putAll(filterProjectMap);

    DBObject group1 = new BasicDBObject();
    group1.put(Fields.UNDERSCORE_ID, Fields.UNDERSCORE_ID_REF);
    addYearlyMonthlyGroupingOperationFirst(filter, group1);
    group1.put("tenderWithAwards", new BasicDBObject("$max", "$tenderWithAwards"));
    group1.put("tenderWithAwardsValue", new BasicDBObject("$max", "$tenderWithAwardsValue"));
    group1.put("tenderAmount", new BasicDBObject("$first", ref(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT)));
    filterProjectMap
            .forEach(
                    (k, v) -> group1
                            .put(k.replace(".", ""),
                                    k.equals("tender.items.classification._id")
                                            ? new BasicDBObject("$first",
                                                    new BasicDBObject("$arrayElemAt",
                                                            Arrays.asList("$" + k, 0)))
                                            : new BasicDBObject("$first", "$" + k)));

    Aggregation agg = Aggregation
            .newAggregation(
                    match(where(MongoConstants.FieldNames.TENDER_STATUS)
                            .is(Tender.Status.active.toString())
                            .and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true)
                            .andOperator(getYearDefaultFilterCriteria(filter,
                                    MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))),
                    getMatchDefaultFilterOperation(filter), new CustomUnwindOperation("$awards", true),
                    new CustomProjectionOperation(project), new CustomGroupingOperation(group1),
                    getTopXFilterOperation(filter, getYearlyMonthlyGroupingFields(filter))
                            .sum("tenderWithAwardsValue").as(Keys.TOTAL_TENDER_AMOUNT).count()
                            .as(Keys.TOTAL_TENDERS).sum("tenderWithAwards").as(Keys.TOTAL_TENDER_WITH_AWARDS),
                    project(Keys.TOTAL_TENDER_AMOUNT, Keys.TOTAL_TENDERS, Keys.TOTAL_TENDER_WITH_AWARDS)
                            .andInclude(
                                    Fields.from(Fields.field(Fields.UNDERSCORE_ID, Fields.UNDERSCORE_ID_REF)))
                            .and(Keys.TOTAL_TENDER_WITH_AWARDS).divide(Keys.TOTAL_TENDERS)
                            .as(Keys.FRACTION_TENDERS_WITH_AWARDS),
                    project(Keys.TOTAL_TENDER_AMOUNT, Keys.TOTAL_TENDERS, Keys.TOTAL_TENDER_WITH_AWARDS,
                            Fields.UNDERSCORE_ID).and(Keys.FRACTION_TENDERS_WITH_AWARDS).multiply(100)
                                    .as(Keys.PERCENTAGE_TENDERS_WITH_AWARDS),
                    (filter.getGroupByCategory() == null ? transformYearlyGrouping(filter) : project())
                            .andInclude(Keys.TOTAL_TENDER_AMOUNT, Keys.TOTAL_TENDERS,
                                    Keys.TOTAL_TENDER_WITH_AWARDS, Keys.PERCENTAGE_TENDERS_WITH_AWARDS),
                    filter.getGroupByCategory() == null ? getSortByYearMonth(filter)
                            : sort(Sort.Direction.DESC, Keys.TOTAL_TENDER_AMOUNT),
                    skip(filter.getSkip()), limit(filter.getPageSize()))
            .withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());

    return releaseAgg(agg);
}

From source file:org.devgateway.ocds.web.rest.controller.TenderPercentagesController.java

@ApiOperation("Returns the percent of tenders with active awards, "
        + "with tender.submissionMethod='electronicSubmission'."
        + "The endpoint also returns the total tenderds with active awards and the count of tenders with "
        + "tender.submissionMethod='electronicSubmission")
@RequestMapping(value = "/api/percentTendersUsingEBid", method = { RequestMethod.POST,
        RequestMethod.GET }, produces = "application/json")
public List<DBObject> percentTendersUsingEBid(@ModelAttribute @Valid final YearFilterPagingRequest filter) {

    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
    project1.put(Fields.UNDERSCORE_ID, "$tender._id");
    project1.put("electronicSubmission", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq",
            Arrays.asList("$tender.submissionMethod", Tender.SubmissionMethod.electronicSubmission.toString())),
            1, 0)));/*from ww w  .  j  a v  a  2 s  .c o m*/

    DBObject group1 = new BasicDBObject();
    group1.put(Fields.UNDERSCORE_ID,
            org.springframework.data.mongodb.core.aggregation.Fields.UNDERSCORE_ID_REF);
    addYearlyMonthlyGroupingOperationFirst(filter, group1);
    group1.put("electronicSubmission", new BasicDBObject("$max", "$electronicSubmission"));

    DBObject group2 = new BasicDBObject();
    addYearlyMonthlyReferenceToGroup(filter, group2);
    group2.put(Keys.TOTAL_TENDERS, new BasicDBObject("$sum", 1));
    group2.put(Keys.TOTAL_TENDERS_USING_EBID, new BasicDBObject("$sum", "$electronicSubmission"));

    DBObject project2 = new BasicDBObject();
    project2.put(Keys.TOTAL_TENDERS, 1);
    project2.put(Keys.TOTAL_TENDERS_USING_EBID, 1);
    project2.put(Keys.PERCENTAGE_TENDERS_USING_EBID,
            new BasicDBObject("$multiply", Arrays.asList(
                    new BasicDBObject("$divide", Arrays.asList("$totalTendersUsingEbid", "$totalTenders")),
                    100)));

    Aggregation agg = newAggregation(
            match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true)
                    .and("tender.submissionMethod.0").exists(true).and(MongoConstants.FieldNames.AWARDS_STATUS)
                    .is(Award.Status.active.toString())
                    .andOperator(getYearDefaultFilterCriteria(filter,
                            MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))),
            unwind("$tender.submissionMethod"), new CustomProjectionOperation(project1),
            new CustomGroupingOperation(group1), new CustomGroupingOperation(group2),
            new CustomProjectionOperation(project2),
            transformYearlyGrouping(filter).andInclude(Keys.TOTAL_TENDERS, Keys.TOTAL_TENDERS_USING_EBID,
                    Keys.PERCENTAGE_TENDERS_USING_EBID),
            getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize()));

    return releaseAgg(agg);
}

From source file:org.devgateway.ocds.web.rest.controller.FundingByLocationController.java

@ApiOperation("Calculates percentage of releases with tender with at least one specified delivery location,"
        + " that is the array tender.items.deliveryLocation has to have items."
        + "Filters out stub tenders, therefore tender.tenderPeriod.startDate has to exist.")
@RequestMapping(value = "/api/qualityFundingByTenderDeliveryLocation", method = { RequestMethod.POST,
        RequestMethod.GET }, produces = "application/json")
public List<DBObject> qualityFundingByTenderDeliveryLocation(
        @ModelAttribute @Valid final YearFilterPagingRequest filter) {

    DBObject project = new BasicDBObject();
    project.putAll(filterProjectMap);/*from   w  w  w  .j a  v a  2  s. com*/
    project.put(Fields.UNDERSCORE_ID, "$tender._id");
    project.put("tenderItemsDeliveryLocation", new BasicDBObject("$cond",
            Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$tender.items.deliveryLocation", 0)), 1, 0)));

    DBObject project1 = new BasicDBObject();
    project1.put(Fields.UNDERSCORE_ID, 0);
    project1.put(Keys.TOTAL_TENDERS_WITH_START_DATE, 1);
    project1.put(Keys.TOTAL_TENDERS_WITH_START_DATE_AND_LOCATION, 1);
    project1.put(Keys.PERCENT_TENDERS_WITH_START_DATE_AND_LOCATION, new BasicDBObject("$multiply",
            Arrays.asList(new BasicDBObject("$divide",
                    Arrays.asList("$totalTendersWithStartDateAndLocation", "$totalTendersWithStartDate")),
                    100)));

    Aggregation agg = newAggregation(
            match(where("tender.tenderPeriod.startDate").exists(true)
                    .andOperator(getYearDefaultFilterCriteria(filter, "tender.tenderPeriod.startDate"))),
            unwind("$tender.items"), new CustomProjectionOperation(project),
            group(Fields.UNDERSCORE_ID_REF).max("tenderItemsDeliveryLocation")
                    .as("hasTenderItemsDeliverLocation"),
            group().count().as("totalTendersWithStartDate").sum("hasTenderItemsDeliverLocation").as(
                    Keys.TOTAL_TENDERS_WITH_START_DATE_AND_LOCATION),
            new CustomProjectionOperation(project1)
    //,skip(filter.getSkip()),limit(filter.getPageSize())
    );

    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> tagCount = results.getMappedResults();
    return tagCount;
}