如何在MongoDB

时间:2018-01-29 10:50:17

标签: spring mongodb spring-boot spring-repositories

我需要在同一个查询中合并切片和计数,让我解释一下:

我有一个集合,用于将评论与他的回复存储在一个数组中

{
    "_id" : ObjectId("5a6b14796ede6d5169ad68a7"),
    "_class" : "com.social.model.comment.FirstLevelComment",
    "contentId" : "5a12996de7e84e0001b93a91",
    "replies" : [ 
        {
            "_id" : ObjectId("5a6b151a6ede6d5169ad68b1"),
            "date" : ISODate("2018-01-26T11:46:34.202Z"),
            "text" : "Reply 1"
        }, 
        {
            "_id" : ObjectId("5a6b151d6ede6d5169ad68b2"),
            "date" : ISODate("2018-01-26T11:46:37.357Z"),
            "text" : "Reply 2"
        }, 
        {
            "_id" : ObjectId("5a6b15206ede6d5169ad68b3"),
            "date" : ISODate("2018-01-26T11:46:40.170Z"),
            "text" : "Reply 3"
        }, 
        {
            "_id" : ObjectId("5a6b15236ede6d5169ad68b4"),
            "date" : ISODate("2018-01-26T11:46:43.025Z"),
            "text" : "Reply 4"
        }, 
        {
            "_id" : ObjectId("5a6b15256ede6d5169ad68b5"),
            "date" : ISODate("2018-01-26T11:46:45.931Z"),
            "text" : "Reply 5"
        }
    ],
    "date" : ISODate("2018-01-26T11:43:53.578Z"),
    "text" : "This is the comment text"
}

每个第一级评论都存储在一个单独的文档中,因此要检索属于内容的所有评论,我必须通过“contentId”字段进行查询匹配。

但是,我只想检索每条评论的前两个回复,所以我必须使用$ slice运算符。

但我必须检索评论的回复总量,我可以在同一个查询中执行此操作吗?

我正在使用带有mongo存储库的spring boot,所以现在我的查询就像这样

@Query(value = "{}", fields = "{ replies : { $slice : 2 }}")
public Page<FirstLevelComment> findByContentId(String contentId, Pageable page);

但不知道如何添加对该查询的回复数量。

修改: 添加查询为Alex P.说

db.comment.aggregate([
{$match:{contentId: "5a12996de7e84e0001b93a91"}},
{
  $project: { 
    _id: 1,
    _class: 1,
    contentId: 1,
    date: 1,
    text: 1,
    countSize: {$size: "$replies"},
    sl: {$slice: ["$replies", 2]}
  }
}])

2 个答案:

答案 0 :(得分:1)

如果你直接在你的mongo服务器上聚合,你必须这样做:

db.collection.aggregate([
{
  $project: { 
    _id: 1,
    _class: 1,
    contentId: 1,
    date: 1,
    text: 1,
    countSize: {$size: "$replies"},
    sl: {$slice: ["$replies", 2]}
  }
}
])

在Java应用程序中使用聚合框架和Spring Data时,不能使用MongoRepository。您必须使用MongoTemplate代替 有关详细信息,请查看documentation

答案 1 :(得分:1)

您可以使用MongoTemplate尝试以下聚合。

ProjectionOperation project = Aggregation.project().and("replies").slice(2).as("first 2 comments").and("replies").size().as("count");
SkipOperation skip  = Aggregation.skip(2L);
LimitOperation limit = Aggregation.limit(5);
Aggregation aggregation = Aggregation.newAggregation(project, skip, limit);
AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, collectionname, Document.class);