【问题标题】:convert mongo shell query to spring data将 mongo shell 查询转换为 spring 数据
【发布时间】:2015-08-01 21:29:04
【问题描述】:

我有一个如下所示的 mongoshell 查询

db.viewedProfile.aggregate(

  {$match : { "viewedMemberId":  "54d6dd15e4b0611ba5762e3d" }},
  {$group : { _id: null,
         total: {$sum: "$count"}}})

我正在努力将其转换为 Spring Data MongoDB。我正在使用弹簧数据版本 1.4.3.RELEASE。聚合构造函数似乎无法识别匹配方法。

【问题讨论】:

  • 请展示您到目前为止所做的尝试。没有显示任何尝试的“转换此代码”问题并不真正受到鼓励。如果您已经做出了一些努力,那么您更有可能获得一些指导。您的问题上有一个Edit 链接供您进行更改。

标签: java spring mongodb spring-data-mongodb


【解决方案1】:

应该这样做:

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

...

Aggregation aggregation = newAggregation(
    ViewedProfile.class,
    match(Criteria.where("viewedMemberId").is("54d6dd15e4b0611ba5762e3d")),
    group().sum("count").as("total")
);

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多