简体   繁体   中英

Can't translate aggregation query from mongodb to Spring Data MongoDb

I've been having trouble trying to transform my query in mongodb into Spring Data MongoDb using Aggregation Object. I have the following document in mongo:

{
"_id" : ObjectId("596ce468798b61179c6442bb"),
"_class" : "com.test.model.User",
"name" : "Oi",
"surName" : "Alo",
"workLogs" : [
    {
        "_id" : ObjectId("596ce468798b61179c6442bc"),
        "day" : 1,
        "month" : 1,
        "year" : 2017,
        "timeEntrance" : "8:00",
        "lunchLeave" : "12:00",
        "lunchBack" : "13:00",
        "timeLeave" : "18:00"
    },
    {
        "_id" : ObjectId("596ce468798b61179c6442bd"),
        "day" : 2,
        "month" : 1,
        "year" : 2017,
        "timeEntrance" : "8:00",
        "lunchLeave" : "12:00",
        "lunchBack" : "13:00",
        "timeLeave" : "18:00"
    }
  ]
}

I would like to query all workLogs which are at the same year and month, and after that i would like to get only the array of workLogs as result. I managed to do it with mongo using this query:

db.user.aggregate([
  {$unwind: '$workLogs'}, 
  {$match: {'workLogs.month':2, 'workLogs.year':2017}}, 
  {$group: {_id:'$_id', workLogs:{$push: '$workLogs'}}}, 
  {$project: {'_id':0, 'workLogs': 1}}
]).pretty()

But I can't find how to translate this query to Spring Data MongoDb, I think I'm almost there, if someone could help me I would appreciate. Here's the code I'm using in java.

Aggregation agg = Aggregation.newAggregation(
  unwind("workLogs"),
  match(Criteria
    .where("_id").is(userId)
    .and("workLogs.month").is(1)
    .and("workLogs.year").is(2017)
    ),
    group("_id"),
    group("horarios")
      .push(new BasicDBObject("workLogs", "workLogs")).as("workLogs"),
    project("workLogs")
);

AggregationResults<WorkLog> results = mongoTemplate.aggregate(agg, "workLogs", WorkLog.class);

Thank you all in advance!

Not sure why you have all the extra fields in your java code.

The java equivalent code for shell query is

 Aggregation agg = Aggregation.newAggregation(
   unwind("workLogs"),
   match(Criteria
      .where("workLogs.month").is(1)
      .and("workLogs.year").is(2017)
   ),
   group("_id").push("workLogs").as("workLogs"),
   project("workLogs").andExclude("_id")
 );

Alternatively, you can simplify your code to use $filter .

import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter;
import static org.springframework.data.mongodb.core.aggregation.BooleanOperators.And.and;
import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq;

 Aggregation agg = newAggregation(project().
     and(
      filter("workLogs").
      as("workLog").
      by(
       and(
          Eq.valueOf("workLog.month").equalToValue(1), 
          Eq.valueOf("workLog.year").equalToValue(2017)
      )
     )
    ).as("workLogs").
    andExclude("_id")
 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM