如何使用mongotemplate或mongo repository(自定义存储库)转换为java mongodb

时间:2015-12-22 15:11:23

标签: spring-data-mongodb

db.profileInterest.aggregate([
   { $project: { _id: 1, fromProfile: 1, toProfile: 1, contactNoViewed:1, shortListed: 1, count: {$size: '$interestStatus'}, threeFavorites: { $slice: [ "$interestStatus", -1 ] }} },
   {$match: {"threeFavorites.id": {$ne: 100}}}
]).pretty();

1 个答案:

答案 0 :(得分:1)

保存List<DBObject>中的聚合输出,如下所示:

AggregationOutput aggregationOutput = mongoTemplate
        .getCollection("collection_name")
        .aggregate(aggregationList);

List<DBObject> dbObjects = (List<DBObject>) aggregationOutput.results();    

在java类型中转换List<DBObject>,如下所示:

for(DBObject dbObject : dbObjects) {
    mongoTemplate.getConverter().read(klass, dbObject);
}