0
votes

I searched in vain and now pose my question to the masses.

  • I have a Collection of GpsRecord, and each GpsRecord belongs to a TrackRecord
  • Each GpsRecord holds an ObjectId referring to its parent TrackRecord
  • I want to query the GpsRecord collection based on a list of TrackRecord.ids

From this query I would like to return a List<List<GpsRecord>> from an aggregate query in MonogDB. My question is how do I correctly define the outputType parameter in .aggregate(agg, GpsRecord.class, List<List<GpsRecord>>)?

@Repository
public class GpsRepositoryImpl implements GpsRepositoryCustom {

    private final MongoTemplate _mongoTemplate;

    @Autowired
    public GpsRepositoryImpl(MongoTemplate mongoTemplate) {
        _mongoTemplate = mongoTemplate;
    }

    @Override
    public List<List<GpsRecord>> aggregate(List<ObjectId> ids) {
        MatchOperation match = getMatchOperation(ids);
        GroupOperation group = getGroupOperation();
        Aggregation agg = Aggregation.newAggregation(match, group);

        return _mongoTemplate
                   .aggregate(agg, GpsRecord.class, List<List<GpsRecord>>)
                   .getMappedResults();
    }

    private MatchOperation getMatchOperation(List<ObjectId> ids) {
        Criteria criteria = Criteria.where("trackRecord.id").in(ids);

        return new MatchOperation(criteria);
    }

    private GroupOperation getGroupOperation() {
        GroupOperation agg = Aggregation.group("trackRecord.id");

        return agg;
    }

}

Edit

Example of a GpsRecord

{
    "_id": ObjectId("593d382c5b3ae715b2cac03d"),
    "_class": "gps",
    "location": {
        "type": "Point",
        "coordinates": [116.315148, 39.984538]
    },
    "dateTime": ISODate("2008-10-23T02:54:40Z"),
    "trackRecord": DBRef("trackRecords", ObjectId("593d382b5b3ae715b2cac029"))
}

Example of a TrackRecord

{
    "_id": ObjectId("593d382b5b3ae715b2cac029"),
    "_class": "track",
    "name": "20081117155223.plt",
    "bbox": {
        "type": "Polygon",
        "coordinates": [[[116.31949,39.999686],[116.325796,39.999686],[116.325796,40.009678],[116.31949,40.009678],[116.31949,39.999686]]]
    }
}

A TrackRecord consists of many GpsRecord objects, as in thousands of them or more.

So based on a List of TrackRecord._id I would like to return a List of List<GpsRecord>.

1
All nice, but this really does not tell anyone what your actual data looks like. Or what sort of parameters are actually input, Or indeed a simple visual representation of the returned data. To reach the wider audience you should show 1. Small selection of documents as viewed from the Mongo Shell, and not writeln or robomongo etc. 2. Same JSON format of expected output "sourced" from the selection of documents provided. I suspect you want something like a GeoJSON Multipoint output just going by the naming. But it's best if you show us instead. - Neil Lunn
I added some example JSON and hope it clarifies things a bit. I don't want to return a GeoJSON just yet, I just want to get the Objects back. In a later stage I will convert it into GeoJSON, I need to manipulate the "raw" dat first before turning it into GeoJSON. Thank you. - Stevan
You can't return the result as a List<List<…>> from just calling MongoOperations.aggregate(…). Query the results and transform these into the representation you're supposed to return. - mp911de
@mp911de dammit, you are totally right! I just realized this myself. I'm sorry for the confusion. So it's not possible to return this query in any other form, then List<...>? - Stevan
You map aggregation results to a specific type and since aggregation results are zero to many you get them as List<…> - mp911de

1 Answers

0
votes

You want such aggregation:

db.GpsRecord.aggregate([
    {$group:{_id:"trackRecord.$id", ...}}
])

Warning: The pipeline cannot operate on values of the following types: Binary, Symbol, MinKey, MaxKey, DBRef, Code, and CodeWScope

But, if you use MongoDB >= 3.4.4, there is a new aggregation command $objectToArray which convert DBRef into array and allows you to group it.

https://docs.mongodb.com/manual/release-notes/3.4/#apr-21-2017