草庐IT

spring - 多重聚合与Unwind - Spring data Mongodb

coder 2023-10-28 原文

我有一个 mongo 查询,用于展开四个对象数组并根据匹配条件过滤数据。我如何在 Spring data mongodb 中执行相同的操作

我曾使用过单一展开,但找不到任何具有多个展开和匹配操作的。

db.generator.aggregate([
{ $unwind :'$eCList'},
{ $unwind :'$pcList'},
{ $unwind :'$cityList'},
{ $unwind :'$eATypeList'},
{ $match : {'eCList.eCCode': { $eq : 'enccode1'} }},
{ $match : {'pcList.pcCode': { $eq : 'pccode1'} }},
{ $match : {'cityList.cityCode': { $eq : 'citycode1'} }},
{ $match : {'eATypeList.eATypeCode': { $eq : 'eATypeCode1'} }},
{ $project : {generatorData : '$generatorData',pcList:'$pcList',
    generatorCode: '$generatorCode', eCId : '$eCList.eCId', 
    eCCode : '$eCList.eCCode', eCKey : '$eCList.eCKey' } }
])

最佳答案

您可以在 1.10.x spring mongo 版本中尝试以下聚合。

 Aggregation aggregation = Aggregation.newAggregation(
       Aggregation.unwind("eCList"),
       Aggregation.unwind("pcList"),
       Aggregation.unwind("cityList"),
       Aggregation.unwind("eATypeList"),
       Aggregation.match(Criteria.where("eCList.eCCode").is("enccode1")),
       Aggregation.match(Criteria.where("pcList.pcCode").is("pccode1")),
       Aggregation.match(Criteria.where("cityList.cityCode").is("citycode1")),
       Aggregation.match(Criteria.where("eATypeList.eATypeCode").is("eATypeCode1")),
       Aggregation.project("generatorData", "pcList", "generatorCode").
          andInclude(Aggregation.bind("eCId", "eCList.eCId")).
          andInclude(Aggregation.bind("eCCode", "eCList.eCCode")).
          andInclude(Aggregation.bind("eCKey", "eCList.eCKey"))
);
List<BasicDBObject>  results  = mongoTemplate.aggregate( aggregation, "generator", BasicDBObject.class).getMappedResults();

生成的 Shell 查询:

{
  "aggregate":"__collection__",
  "pipeline":[
    {"$unwind":"$eCList"},
    {"$unwind":"$pcList"},
    {"$unwind":"$cityList"},
    {"$unwind":"$eATypeList"},
    {"$match":{"eCList.eCCode":"enccode1"}},
    {"$match":{"pcList.pcCode":"pccode1"}},
    {"$match":{"cityList.cityCode":"citycode1"}},
    {"$match":{"eATypeList.eATypeCode":"eATypeCode1"}},
    {"$project":{
      "generatorData":1,
      "pcList":1,
      "generatorCode":1,
      "eCId":"$eCList.eCId",
      "eCCode":"$eCList.eCCode",
      "eCKey":"$eCList.eCKey"}}
  ]
}

使用静态导入

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

Aggregation aggregation = newAggregation(
     unwind("eCList"),
     unwind("pcList"),
     unwind("cityList"),
     unwind("eATypeList"),
     match(where("eCList.eCCode").is("enccode1")),
     match(where("pcList.pcCode").is("pccode1")),
     match(where("cityList.cityCode").is("citycode1")),
     match(where("eATypeList.eATypeCode").is("eATypeCode1")),
     project("generatorData", "pcList", "generatorCode").
       andInclude(bind("eCId", "eCList.eCId")).
       andInclude(bind("eCCode", "eCList.eCCode")).
       andInclude(bind("eCKey", "eCList.eCKey"))
);

关于spring - 多重聚合与Unwind - Spring data Mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47974638/

有关spring - 多重聚合与Unwind - Spring data Mongodb的更多相关文章

  1. ruby-on-rails - 带 Spring 锁的 Rails 4 控制台 - 2

    我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.

  2. spring.profiles.active和spring.profiles.include的使用及区别说明 - 2

    转自:spring.profiles.active和spring.profiles.include的使用及区别说明下文笔者讲述spring.profiles.active和spring.profiles.include的区别简介说明,如下所示我们都知道,在日常开发中,开发|测试|生产环境都拥有不同的配置信息如:jdbc地址、ip、端口等此时为了避免每次都修改全部信息,我们则可以采用以上的属性处理此类异常spring.profiles.active属性例:配置文件,可使用以下方式定义application-${profile}.properties开发环境配置文件:application-dev

  3. ruby-on-rails - Spring 不起作用。 [未初始化常量 Spring::SID::DL] - 2

    我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li

  4. ruby - Rails Elasticsearch 聚合 - 2

    不知何故,我似乎无法获得包含我的聚合的响应...使用curl它按预期工作:HBZUMB01$curl-XPOST"http://localhost:9200/contents/_search"-d'{"size":0,"aggs":{"sport_count":{"value_count":{"field":"dwid"}}}}'我收到回复:{"took":4,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":90,"max_score":0.0,"hits":[]},"a

  5. c# - Ruby 等效于 C# Linq 聚合方法 - 2

    什么是Linq聚合方法的ruby​​等价物。它的工作原理是这样的varfactorial=new[]{1,2,3,4,5}.Aggregate((acc,i)=>acc*i);每次将数组序列中的值传递给lambda时,变量acc都会累积。 最佳答案 这在数学以及几乎所有编程语言中通常称为折叠。它是更普遍的变形概念的一个实例。Ruby从Smalltalk中继承了这个特性的名称,它被称为inject:into:(像aCollectioninject:aStartValueinto:aBlock一样使用。)所以,在Ruby中,它称为inj

  6. 【云原生】SpringCloud-Spring Boot Starter使用测试 - 2

    目录SpringBootStarter是什么?以前传统的做法使用SpringBootStarter之后starter的理念:starter的实现: 创建SpringBootStarter步骤在idea新建一个starter项目、直接执行下一步即可生成项目。 在xml中加入如下配置文件:创建proterties类来保存配置信息创建业务类:创建AutoConfiguration测试如下:SpringBootStarter是什么? SpringBootStarter是在SpringBoot组件中被提出来的一种概念、简化了很多烦琐的配置、通过引入各种SpringBootStarter包可以快速搭建出一

  7. Spring Boot集成ElasticSearach - 2

    文章目录前言一、Elasticsearch版本介绍二、客户端种类三、客户端与版本兼容性四、引入Elasticsearch依赖包五、客户端配置六、Elasticsearch使用前言ElasticSearch是Elastic公司出品的一款功能强大的搜索引擎,被广泛的应用于各大IT公司,它的代码位于https://github.com/elastic/elasticsearch,目前是一个开源项目。ElasticSearch公司的另外两个开源产品Logstash、Kibana与ElasticSearch构成了著名的ELK技术栈。。他们三个共同形成了一个强大的生态圈。简单地说,Logstash负责数据

  8. Spring Security 6.0系列【32】授权服务器篇之默认过滤器 - 2

    有道无术,术尚可求,有术无道,止于术。本系列SpringBoot版本3.0.4本系列SpringSecurity版本6.0.2本系列SpringAuthorizationServer版本1.0.2源码地址:https://gitee.com/pearl-organization/study-spring-security-demo文章目录前言1.OAuth2AuthorizationServerMetadataEndpointFilter2.OAuth2AuthorizationEndpointFilter3.OidcProviderConfigurationEndpointFilter4.N

  9. IDEA 2022 创建 Spring Boot 项目详解 - 2

    如何用IDEA2022创建并初始化一个SpringBoot项目?目录如何用IDEA2022创建并初始化一个SpringBoot项目?0. 环境说明1.  创建SpringBoot项目 2.编写初始化代码0. 环境说明IDEA2022.3.1JDK1.8SpringBoot1.  创建SpringBoot项目        打开IDEA,选择NewProject创建项目。        填写项目名称、项目构建方式、jdk版本,按需要修改项目文件路径等信息。        选择springboot版本以及需要的包,此处只选择了springweb。        此处需特别注意,若你使用的是jdk1

  10. ruby-on-rails - 您已经激活了 spring 1.3.6,但是您的 Gemfile 需要 spring 1.3.3。 ( gem ::加载错误) - 2

    我今天遇到了同样的问题,有一个建议:在您的命令前添加bundleexec可能会解决此问题。前置bundleexec没有帮助(我已经这样做了)。springstop和springrestart没有帮助。我需要做的:bundleupdatespring这对我有用。在之前的gemlock文件中使用spring版本是否有更好的解决方案? 最佳答案 我删除gemfile.lock并运行bundle通常会清除一切。否则只需从Gemfile中删除gem"spring"并运行bundle 关于ruby-

随机推荐