Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 弹簧&x2B;MongoDB标记@Query,其中$group不起作用_Spring_Mongodb - Fatal编程技术网

Spring 弹簧&x2B;MongoDB标记@Query,其中$group不起作用

Spring 弹簧&x2B;MongoDB标记@Query,其中$group不起作用,spring,mongodb,Spring,Mongodb,注意:向下查看已编辑的消息 我试着模仿这个问题: db.sentiments.aggregate([ {"$group" : {_id:{theme_id:"$theme",sentiment_id:"$sentiment"}, count:{$sum:1}}}, {"$sort":{"_id.theme_id":1}} ]) 这是我为了模仿它而生成的代码: @RepositoryRestResource(collectionResourceRel = "sentiments", pa

注意:向下查看已编辑的消息

我试着模仿这个问题:

db.sentiments.aggregate([
{"$group" : {_id:{theme_id:"$theme",sentiment_id:"$sentiment"}, count:{$sum:1}}},
{"$sort":{"_id.theme_id":1}} ])
这是我为了模仿它而生成的代码:

    @RepositoryRestResource(collectionResourceRel = "sentiments", path = "sentiments")
public interface SentimentsRepository extends MongoRepository<Sentiments, String> {
    Long countByTheme(@Param("theme") String theme);

@Query(value ="[\n" +
        "    {\"$group\" : {_id:{theme_id:\"$theme\",sentiment_id:\"$sentiment\"}, count:{$sum:1}}},\n" +
        "\t{\"$sort\":{\"_id.theme_id\":1}}\n" +
        "]",count = true)
List<Object> comptarSentiments();
事实上,我是一个乞丐,用什么来形容春天,所以我很迷茫,有人知道我该怎么做吗

谢谢,对不起,我的英语不好,不是我的母语

[编辑]---------------------------------------- 正如肖恩·克拉克(Shawn Clark)所写的评论,这样做是不可能的,为了实现这一点,您需要创建一个customRepository

我一直在尝试这样做,但有些地方似乎不正确,下面是我的新代码:

@RepositoryRestResource(collectionResourceRel = "sentiments", path = "sentiments")
public interface SentimentsRepository extends CrudRepository<Sentiments, String>, CustomSentimentsRepository {

//Other methods...

}

public interface CustomSentimentsRepository {

    List<CountResult> yourCustomMethod();

    class CountResult{
        String theme;
        String sentiment;
        int total;
    }
}

public class SentimentsRepositoryImpl implements CustomSentimentsRepository {
    private final MongoOperations operations;


    @Autowired
    public SentimentsRepositoryImpl(MongoOperations operations) {

        Assert.notNull(operations, "MongoOperations must not be null!");
        this.operations = operations;
    }

    @Override
    public List<CountResult> yourCustomMethod(){
        Aggregation agg = Aggregation.newAggregation(
                Aggregation.group("theme","sentiment").count().as("total"),
                Aggregation.project("theme","sentiment").and("total").previousOperation(),
                Aggregation.sort(Sort.Direction.DESC, "theme")
        );

        //Convert the aggregation result into a List
        AggregationResults<CountResult> groupResults
                = operations.aggregate(agg,"sentiments",  CountResult.class);
        //List<CountResult> result = groupResults.getMappedResults();

        return groupResults.getMappedResults();
    }
}
@RepositoryRestResource(collectionResourceRel=“情感”,path=“情感”)
公共界面情感假设扩展了crudepository,customepository{
//其他方法。。。
}
公共界面客户情感假设{
列出您的CustomMethod();
类计数结果{
弦乐主题;
弦情;
整数合计;
}
}
公共类情感RepositoryImpl实现CustomMountainRespository{
私人最终运营;
@自动连线
公众情绪储备基金(MongoOperations operations){
notNull(操作,“MongoOperations不得为null!”);
这是操作=操作;
}
@凌驾
公共列表自定义方法(){
聚合agg=Aggregation.newAggregation(
聚合.组(“主题”、“情感”).count().作为(“总计”),
聚合。项目(“主题”、“情感”)和(“总计”)。之前的操作(),
聚合.sort(sort.Direction.DESC,“主题”)
);
//将聚合结果转换为列表
聚合结果groupResults
=operations.aggregate(agg,“情绪”,CountResult.class);
//List result=groupResults.getMappedResults();
返回groupResults.getMappedResults();
}
}

我甚至不能破解这个代码,我总是得到一个404

根据我发现的信息,你不能在MongoRepository方法上进行复杂的@查询。在这种情况下,您需要创建一个类,并使用mongoTemplate实现comptarMotions()方法,以使用聚合函数查询数据存储。然后创建一个公开REST端点的控制器类,并让它调用存储库

一旦您开始在Mongo中执行复杂的查询,您就失去了@RepositoryRestResource的易用性,必须自己将REST端点连接到存储库


我终于设法解决了这个问题,似乎它与控制器和innerClass CountResult中的atribute“total”类型有关,它需要是一个字符串(这非常重要,否则Aggregation.project将失败)。最后的代码如下:

public interface CustomSentimentsRepository {

    List<CountResult> myCountGroupByThemeAndSentiment();

    class CountResult{
        public String theme;
        public String sentiment;
        public String total;
    }
}

public class SentimentsRepositoryImpl implements CustomSentimentsRepository {
    private final MongoTemplate mongoTemplate;


    @Autowired
    public SentimentsRepositoryImpl(MongoTemplate mongoTemplate) {

        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public List<CountResult> myCountGroupByThemeAndSentiment(){
        Aggregation agg = Aggregation.newAggregation(
                Aggregation.group("theme","sentiment").count().as("total"),
                Aggregation.project("theme","sentiment").andInclude("total"),


        Aggregation.sort(Sort.Direction.ASC,"theme","sentiment")
    );


    AggregationResults<CountResult> groupResults
            = mongoTemplate.aggregate(agg,"sentiments",  CountResult.class);


        return groupResults.getMappedResults();
    }
}

@RepositoryRestResource(collectionResourceRel = "sentiments", path = "sentiments")
public interface SentimentsRepository extends CrudRepository<Sentiments, String>, CustomSentimentsRepository {
    //Other methods

}

@RestController
@RequestMapping(value = "sentiments/search")
public class ChartsController {
    @Autowired
    private SentimentsRepository sentimentsRepository;

    @RequestMapping(value = "myCountGroupByThemeAndSentiment", method = RequestMethod.GET)
    public ResponseEntity<?> yourCustomMethod() {
        List<?> count=sentimentsRepository.myCountGroupByThemeAndSentiment();
        return new ResponseEntity(count, HttpStatus.OK);
    }

}
公共界面客户情感推荐{
按MeansEntity()列出MyCountGroups;
类计数结果{
公共字符串主题;
公众情绪;
公共字符串总数;
}
}
公共类情感RepositoryImpl实现CustomMountainRespository{
私有最终MongoTemplate MongoTemplate;
@自动连线
公众情绪建议(MongoTemplate MongoTemplate){
this.mongoTemplate=mongoTemplate;
}
@凌驾
公共列表MyCountGroupByMeansEntity(){
聚合agg=Aggregation.newAggregation(
聚合.组(“主题”、“情感”).count().作为(“总计”),
聚合。项目(“主题”、“情感”)和包括(“总计”),
聚合.sort(sort.Direction.ASC,“主题”、“情感”)
);
聚合结果groupResults
=mongoTemplate.aggregate(agg,“情绪”,CountResult.class);
返回groupResults.getMappedResults();
}
}
@RepositoryRestResource(collectionResourceRel=“情感”,path=“情感”)
公共界面情感假设扩展了crudepository,customepository{
//其他方法
}
@RestController
@请求映射(value=“情感/搜索”)
公共类图表控制器{
@自动连线
私人情感的消极情感的消极情感;
@RequestMapping(value=“myCountGroupByMeansEntity”,method=RequestMethod.GET)
公共响应性方法(){
列表计数=情感存储。MyCountGroupByMeansEntity();
返回新的ResponseEntity(count,HttpStatus.OK);
}
}

您可以使用spring data mongodb 2.2.X版本中提供的@Agrgation:


@Aggregation(pipeline={{{'$group':{''u id':'$lastname',name:{$addToSet:'$?0'}}}},“{'$sort':{'lastname':-1}}}”)列表groupbylastname和(字符串属性)

您好,看起来您是对的,但我仍然遇到问题,请您查看编辑消息好吗?非常感谢。您如何尝试呼叫端点?是什么给了你404?你有控制器类的代码可以共享吗?你又对了,我的控制器中的一个拼写错误导致了这个问题,我感到有点惭愧,非常感谢你的关注。;)
public interface CustomSentimentsRepository {

    List<CountResult> myCountGroupByThemeAndSentiment();

    class CountResult{
        public String theme;
        public String sentiment;
        public String total;
    }
}

public class SentimentsRepositoryImpl implements CustomSentimentsRepository {
    private final MongoTemplate mongoTemplate;


    @Autowired
    public SentimentsRepositoryImpl(MongoTemplate mongoTemplate) {

        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public List<CountResult> myCountGroupByThemeAndSentiment(){
        Aggregation agg = Aggregation.newAggregation(
                Aggregation.group("theme","sentiment").count().as("total"),
                Aggregation.project("theme","sentiment").andInclude("total"),


        Aggregation.sort(Sort.Direction.ASC,"theme","sentiment")
    );


    AggregationResults<CountResult> groupResults
            = mongoTemplate.aggregate(agg,"sentiments",  CountResult.class);


        return groupResults.getMappedResults();
    }
}

@RepositoryRestResource(collectionResourceRel = "sentiments", path = "sentiments")
public interface SentimentsRepository extends CrudRepository<Sentiments, String>, CustomSentimentsRepository {
    //Other methods

}

@RestController
@RequestMapping(value = "sentiments/search")
public class ChartsController {
    @Autowired
    private SentimentsRepository sentimentsRepository;

    @RequestMapping(value = "myCountGroupByThemeAndSentiment", method = RequestMethod.GET)
    public ResponseEntity<?> yourCustomMethod() {
        List<?> count=sentimentsRepository.myCountGroupByThemeAndSentiment();
        return new ResponseEntity(count, HttpStatus.OK);
    }

}