本文介绍了将MongoDB查询转换为Spring MongoDB语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我无法将以下mongoDB查询转换为spring查询,我尝试了多种方法,但未得到结果.

Hello I am unable to convert the following mongoDB query into spring query, i have tried multiple ways, but did not get the result.

db.getCollection('FarmerCropDataLog').aggregate([
        {
            "$match" :
            {
                "cropData.crop" : "RICE",
                 "creationTime" :
                  {
                      $lt  : 1551447981473.0
                  }
            }
        },
        {
            "$group" :
            {
                _id : null,
                "average" :{
                        $avg : "$cropData.cropPrice"
                },
                "max" :{
                        $max : "$cropData.cropPrice"
                },
                "min":{
                        $min : "$cropData.cropPrice"
                }
            }
        }
    ])

我已经写了下面的代码,但是无法考虑下一步.

I have written follwing code, but unable to think about next step.

Query query = new Query();

query.addCriteria(Criteria.where(FarmerCropDataLog.Constants.CROP_LOG).elemMatch(Criteria.where(CropData.Constants.CROP).is(getComparisonSheet.getCrop())));

query.addCriteria(Criteria.where(FarmerCropDataLog.Constants.CREATION_TIME).gt(Year * DIFF));

推荐答案

您是否曾经考虑过使用MongoDB指南针?它将使您的工作非常简单.

Have you ever thought about using MongoDB compass? It will make your work very simple.

  1. 打开MongoDB compass连接到您的实例
  2. 聚合"标签,构建管道
  3. 单击save pipeline选项旁边的3个点(...)
  4. 选择export to language并选择Java
  5. 您的查询已准备就绪
  1. Open MongoDB compass connect to your instance
  2. Aggregation tab, construct your pipeline
  3. click on the 3 dots(...) next to save pipeline option
  4. Select export to language and select Java
  5. Your query is ready

这是Java查询

Arrays.asList(match(and(eq("cropData.crop", "RICE"), lt("creationTime", 1551447981473.0d))), group(new BsonNull(), avg("average", "$cropData.cropPrice"), max("max", "$cropData.cropPrice"), min("min", "$cropData.cropPrice")))

这篇关于将MongoDB查询转换为Spring MongoDB语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 21:36