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

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

Introduction

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

Prototype

String UNDERSCORE_ID

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

Click Source Link

Usage

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

@ApiOperation(value = "Calculates the average tender period, per each year. The year is taken from "
        + "tender.tenderPeriod.startDate and the duration is taken by counting the days"
        + "between tender.tenderPeriod.endDate and tender.tenderPeriod.startDate")
@RequestMapping(value = "/api/averageTenderPeriod", method = { RequestMethod.POST,
        RequestMethod.GET }, produces = "application/json")
public List<DBObject> averageTenderPeriod(@ModelAttribute @Valid final YearFilterPagingRequest filter) {

    DBObject tenderLengthDays = new BasicDBObject("$divide",
            Arrays.asList(new BasicDBObject("$subtract",
                    Arrays.asList(ref(MongoConstants.FieldNames.TENDER_PERIOD_END_DATE),
                            ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))),
                    DAY_MS));//from w  w  w  .j a  va 2s  .  co m

    DBObject project = new BasicDBObject();
    project.put(Fields.UNDERSCORE_ID, 0);
    addYearlyMonthlyProjection(filter, project, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
    project.put("tenderLengthDays", tenderLengthDays);

    Aggregation agg = newAggregation(
            match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true)
                    .and(MongoConstants.FieldNames.TENDER_PERIOD_END_DATE).exists(true)
                    .andOperator(getYearDefaultFilterCriteria(filter,
                            MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))),
            new CustomProjectionOperation(project),
            getYearlyMonthlyGroupingOperation(filter).avg("$tenderLengthDays").as(Keys.AVERAGE_TENDER_DAYS),
            transformYearlyGrouping(filter).andInclude(Keys.AVERAGE_TENDER_DAYS), getSortByYearMonth(filter),
            skip(filter.getSkip()), limit(filter.getPageSize()));

    return releaseAgg(agg);
}

From source file:it.f2informatica.mongodb.repositories.impl.ConsultantRepositoryImpl.java

@Override
public int removeExperience(String consultantId, String experienceId) {
    Query query = new Query(
            where(ID).is(consultantId).and(EXPERIENCES + "." + Fields.UNDERSCORE_ID).is(experienceId));
    Update update = new Update().pull(EXPERIENCES, findExperience(consultantId, experienceId));
    return updateConsultant(query, update).getN();
}

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

/**
 * db.release.aggregate( [ {$match: {MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT: {$exists:
 * true}}}, {$unwind:"$awards"},//from w w  w . j  a  v a 2  s . c  o  m
 * {$project:{_id:0,MongoConstants.FieldNames.AWARDS_DATE:1,
 * "awards.suppliers.name":1,"awards.value":1, "planning.budget":1}},
 * {$sort: {MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT:-1}}, {$limit:10} ] )
 *
 * @return
 */

@ApiOperation(value = "Returns the top ten largest active awards."
        + " The amount is taken from the award.value field. The returned data will contain"
        + "the following fields: " + "awards.date, awards.suppliers.name, "
        + "awards.value.amount, awards.suppliers.name, planning.budget (if any)")
@RequestMapping(value = "/api/topTenLargestAwards", method = { RequestMethod.POST,
        RequestMethod.GET }, produces = "application/json")
public List<DBObject> topTenLargestAwards(@ModelAttribute @Valid final YearFilterPagingRequest filter) {

    BasicDBObject project = new BasicDBObject();
    project.put(Fields.UNDERSCORE_ID, 0);
    project.put(MongoConstants.FieldNames.AWARDS_DATE, 1);
    project.put("awards.suppliers.name", 1);
    project.put(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT, 1);
    project.put("planning.budget", 1);

    Aggregation agg = newAggregation(
            match(where(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT).exists(true)
                    .and(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString())
                    .andOperator(getDefaultFilterCriteria(filter))),
            unwind("awards"),
            match(getYearFilterCriteria(filter.awardFiltering(), MongoConstants.FieldNames.AWARDS_DATE)),
            new CustomOperation(new BasicDBObject("$project", project)),
            sort(Direction.DESC, MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT), limit(10));

    return releaseAgg(agg);
}

From source file:it.f2informatica.mongodb.repositories.impl.ConsultantRepositoryImpl.java

@Override
public int updateEducation(Education education, String consultantId) {
    Query query = new Query(
            where(ID).is(consultantId).and(EDUCATIONS + "." + Fields.UNDERSCORE_ID).is(education.getId()));
    Update update = new Update().set(EDUCATIONS + ".$", education);
    return updateConsultant(query, update).getN();
}

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

@ApiOperation(value = "Counts the indicators that are flagged, across all releases. If one "
        + "indicator has multiple types is only counted once, so this is different from flaggedStats and "
        + "cannot be reproduced by just summing up all types in flaggedStats!")
@RequestMapping(value = "/api/totalFlags", method = { RequestMethod.POST,
        RequestMethod.GET }, produces = "application/json")
public List<DBObject> totalFlags(final YearFilterPagingRequest filter) {

    Aggregation agg = newAggregation(/*from  w w  w  . ja va2 s . co  m*/
            match(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE)),
            project().and(FLAGS_TOTAL_FLAGGED).as("totalFlagged"),
            group().sum("totalFlagged").as(Keys.FLAGGED_COUNT),
            project(Keys.FLAGGED_COUNT).andExclude(Fields.UNDERSCORE_ID));

    return releaseAgg(agg);
}

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

@ApiOperation(value = "Quality indicator for averageTenderPeriod endpoint, "
        + "showing the percentage of tenders that have start and end dates vs the total tenders in the system")
@RequestMapping(value = "/api/qualityAverageTenderPeriod", method = { RequestMethod.POST,
        RequestMethod.GET }, produces = "application/json")
public List<DBObject> qualityAverageTenderPeriod(
        @ModelAttribute @Valid final DefaultFilterPagingRequest filter) {

    DBObject project = new BasicDBObject();
    project.put(Fields.UNDERSCORE_ID, 0);
    project.put("tenderWithStartEndDates",
            new BasicDBObject("$cond", Arrays.asList(
                    new BasicDBObject("$and", Arrays.asList(
                            new BasicDBObject("$gt",
                                    Arrays.asList(ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE),
                                            null)),
                            new BasicDBObject("$gt", Arrays
                                    .asList(ref(MongoConstants.FieldNames.TENDER_PERIOD_END_DATE), null)))),
                    1, 0)));/* w w  w  .ja  v  a  2 s . c o m*/

    DBObject project1 = new BasicDBObject();
    project1.put(Keys.TOTAL_TENDER_WITH_START_END_DATES, 1);
    project1.put(Keys.TOTAL_TENDERS, 1);
    project1.put(Keys.PERCENTAGE_TENDER_WITH_START_END_DATES, new BasicDBObject("$multiply", Arrays.asList(
            new BasicDBObject("$divide", Arrays.asList("$totalTenderWithStartEndDates", "$totalTenders")),
            100)));

    Aggregation agg = newAggregation(match(getDefaultFilterCriteria(filter)),
            new CustomProjectionOperation(project), group().sum("tenderWithStartEndDates")
                    .as(Keys.TOTAL_TENDER_WITH_START_END_DATES).count().as(Keys.TOTAL_TENDERS),
            new CustomProjectionOperation(project1));

    return releaseAgg(agg);
}

From source file:it.f2informatica.mongodb.repositories.impl.ConsultantRepositoryImpl.java

@Override
public int removeEducation(String consultantId, String educationId) {
    Query query = new Query(
            where(ID).is(consultantId).and(EDUCATIONS + "." + Fields.UNDERSCORE_ID).is(educationId));
    Update update = new Update().pull(EDUCATIONS, findEducation(consultantId, educationId));
    return updateConsultant(query, update).getN();
}

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

private List<DBObject> totalIndicatorsByIndicatorType(String statsProperty,
        final YearFilterPagingRequest filter) {

    Aggregation agg = newAggregation(// ww w.  j a va  2s. c  om
            match(where("flags." + statsProperty + ".0").exists(true).andOperator(
                    getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))),
            unwind("flags." + statsProperty), project("flags." + statsProperty),
            group(statsProperty + ".type").sum(statsProperty + ".count").as(Keys.INDICATOR_COUNT),
            project(Keys.INDICATOR_COUNT).and(Fields.UNDERSCORE_ID).as(Keys.TYPE).andExclude(

                    Fields.UNDERSCORE_ID),
            sort(Sort.Direction.ASC, "type"));

    return releaseAgg(agg);
}

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

@ApiOperation(value = "Counts the tenders/awards where the given supplier id is among the winners. "
        + "This assumes there is only  one active award, which always seems to be the case, per tender. ")
@RequestMapping(value = "/api/activeAwardsCount", method = { RequestMethod.POST,
        RequestMethod.GET }, produces = "application/json")
public List<DBObject> activeAwardsCount(@ModelAttribute @Valid final YearFilterPagingRequest filter) {

    Aggregation agg = newAggregation(/*ww  w.j a v  a 2 s  . c  o m*/
            match(where(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString()).andOperator(
                    getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))),
            unwind("awards"),
            match(where(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString())
                    .andOperator(getYearDefaultFilterCriteria(filter.awardFiltering(),
                            MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))),
            unwind("awards.suppliers"), group(MongoConstants.FieldNames.AWARDS_SUPPLIERS_ID).count().as("cnt"),
            project("cnt").and(Fields.UNDERSCORE_ID).as("supplierId").andExclude(Fields.UNDERSCORE_ID));

    return releaseAgg(agg);
}