我想更改 return 类型的 AggregationResults
I want to change the return type of AggregationResults
我目前正在通过 Spring 引导使用 mongoTemplate
,我的数据结构是:
我写了下面的代码来只获取数据库中的评论列表。
MatchOperation matchOperation = Aggregation.match(
Criteria.where("_id").is(new ObjectId(postId))
);
ProjectionOperation projectionOperation = Aggregation.project()
.and("comments").as("comments");
Aggregation aggregation = Aggregation.newAggregation(matchOperation, projectionOperation);
AggregationResults<Object> result = mongoTemplate.aggregate(aggregation, PostEntity.class, Object.class);
result.getMappedResults().forEach(System.out::println);
System.out::println
内容如下
{
_id=618b37bfb6196619dbe35abb,
comments=[
{
_id=618b65c64d04820f90565c70,
writer=617a2d81c4033d1358e2ffba,
nickname=test user,
createDate=Wed Nov 10 15:25:10 KST 2021,
content=qwer,
replies=[],
likes=[]
},
{
_id=618b66784d04820f90565c71,
writer=617a2d81c4033d1358e2ffba,
nickname=test user2,
createDate=Wed Nov 10 15:28:08 KST 2021,
content=asdf,
replies=[],
likes=[]
},
{
_id=618b67d54d04820f90565c72,
writer=617a2d81c4033d1358e2ffba,
nickname=test user3,
createDate=Wed Nov 10 15:33:57 KST 2021,
content=asdf,
replies=[],
likes=[]
},
...
]
}
数据导入没问题,就是不知道怎么处理。我想创建一个CommentDto
对象,把上面的输出注释列表放在List<CommentDto>
里,怎么办?
当您 运行 查询映射到对象时。只需将输出对象类型更改为您的 dto:
AggregationResults<CommentDto> result = mongoTemplate.aggregate(aggregation, PostEntity.class, CommentDto.class);
您也可以将其作为列表检索:
Aggregation aggregation = Aggregation.newAggregation(lookUp, match1, unwindPrices, unwindTags, unwindIndex, match2, groupOperation);
List<ProductVo> list = mongotemplate.aggregate(aggregation ,"products", ProductVo.class).getMappedResults();
我目前正在通过 Spring 引导使用 mongoTemplate
,我的数据结构是:
我写了下面的代码来只获取数据库中的评论列表。
MatchOperation matchOperation = Aggregation.match(
Criteria.where("_id").is(new ObjectId(postId))
);
ProjectionOperation projectionOperation = Aggregation.project()
.and("comments").as("comments");
Aggregation aggregation = Aggregation.newAggregation(matchOperation, projectionOperation);
AggregationResults<Object> result = mongoTemplate.aggregate(aggregation, PostEntity.class, Object.class);
result.getMappedResults().forEach(System.out::println);
System.out::println
内容如下
{
_id=618b37bfb6196619dbe35abb,
comments=[
{
_id=618b65c64d04820f90565c70,
writer=617a2d81c4033d1358e2ffba,
nickname=test user,
createDate=Wed Nov 10 15:25:10 KST 2021,
content=qwer,
replies=[],
likes=[]
},
{
_id=618b66784d04820f90565c71,
writer=617a2d81c4033d1358e2ffba,
nickname=test user2,
createDate=Wed Nov 10 15:28:08 KST 2021,
content=asdf,
replies=[],
likes=[]
},
{
_id=618b67d54d04820f90565c72,
writer=617a2d81c4033d1358e2ffba,
nickname=test user3,
createDate=Wed Nov 10 15:33:57 KST 2021,
content=asdf,
replies=[],
likes=[]
},
...
]
}
数据导入没问题,就是不知道怎么处理。我想创建一个CommentDto
对象,把上面的输出注释列表放在List<CommentDto>
里,怎么办?
当您 运行 查询映射到对象时。只需将输出对象类型更改为您的 dto:
AggregationResults<CommentDto> result = mongoTemplate.aggregate(aggregation, PostEntity.class, CommentDto.class);
您也可以将其作为列表检索:
Aggregation aggregation = Aggregation.newAggregation(lookUp, match1, unwindPrices, unwindTags, unwindIndex, match2, groupOperation);
List<ProductVo> list = mongotemplate.aggregate(aggregation ,"products", ProductVo.class).getMappedResults();