Search code examples
javaspringmongodbaggregation-frameworkprojection

MongoTemplate aggregation returns no results after operation


I am trying to get some raw data from the mongo collection. I have two similar collections with the same data. When I try to get aggregation results with DBObject list, from first collection data is returned fully but from the second one return nothing. But I need to return data from the second collection.

From my observations, the first collection has documents with field "_class" and another hasn't. In the pipeline's end after projection data similar. I stubbed.

//Document from the first collection

{
    "_id" : ObjectId("5cdd7a0848d4ce0001239b62"),
    "_class" : "com.my.proj.entity.MovementEvent",
    "zoneId" : ObjectId("54e70a7b2cdcc65ef6d80eff"),
    "direction" : "OUT",
    "transportId" : "AAAA",
    "eventDate" : ISODate("2019-05-16T14:56:07Z"),
    "lastUpdated" : ISODate("2019-05-16T14:56:08.571Z")
}

//Document from the second collection

{
    "_id" : ObjectId("5955daa8d87a127b75154c7d"),
    "zoneId" : ObjectId("54e70a7b2cdcc65ef6d80eff"),
    "transportId" : "AAAA",
    "currentDirection" : "OUT",
    "eventDate" : ISODate("2019-05-16T14:56:07Z"),
    "serviceFlag" : false,
    "lastUpdated" : ISODate("2019-05-16T14:56:08.570Z")
}

//Projection object:

ProjectionOperation projectionStage = Aggregation.project()
                .and(PROJECTION_ELEMENT + "._" + MovementEvent.ID_FIELD).as("_" + MovementEvent.ID_FIELD)
                .and(PROJECTION_ELEMENT + "." + MovementEvent.DIRECTION).as(MovementEvent.DIRECTION)
                .and(PROJECTION_ELEMENT + "." + MovementEvent.EVENT_DATE).as(MovementEvent.EVENT_DATE)
                .and(PROJECTION_ELEMENT + "." + MovementEvent.LAST_UPDATED_FIELD).as(MovementEvent.LAST_UPDATED_FIELD)
                .and(PROJECTION_ELEMENT + "." + MovementEvent.TRANSPORT_ID).as(MovementEvent.TRANSPORT_ID)
                .and(PROJECTION_ELEMENT + "." + MovementEvent.ZONE_ID).as(MovementEvent.ZONE_ID);

// mongo document after aggregation same for both collection

{
    "_id" : ObjectId("5955daa8d87a127b75154c7d"),
    "zoneId" : ObjectId("54e70a7b2cdcc65ef6d80eff"),
    "transportId" : "AAAA",
    "currentDirection" : "OUT",
    "eventDate" : ISODate("2019-05-16T14:56:07Z"),
    "lastUpdated" : ISODate("2019-05-16T14:56:08.570Z")
}

// aggregation pipeline

AggregationResults<DBObject> result1 = mongoTemplate.aggregate(
                Aggregation.newAggregation(matchStage, sortStage, groupStage,projectionStage), "lastMovementFlag",DBObject.class);

// request to mongo has form like one below

db.lastMovementFlag.aggregate([ 
{$match:{$and:[{transportId:"AAAA"},{$or:[{"zoneId":ObjectId("54e70a7b2cdcc65ef6d80eff")},{zoneId:ObjectId("54e70b232cdcc65ef6d80f00")}]}]}}, 
{$sort:{eventDate:1}}, 
{$group:{_id:{transportId:"$transportId"}, last:{$last:"$$ROOT"}}},
{$project:{_id:"$last._id", _class:"$last._class",direction:"$last.currentDirection",eventDate:"$last.eventDate",lastUpdated:"$last.lastUpdated",transportId:"$last.transportId", zoneId:"$last.zoneId"}} 
])

I can retrieve data from "Movements" collection but from "lastMovementFlag" can't. AggregationResults is empty.


Solution

  • It was my silly mistake due to inattention. My object Criteria had a condition with required field "direction" but in the second collection that field has name "currentDirection". Therefore aggregation didn't give any results.