Spring Data Mongo Embedded 测试失败并出现“无法识别的管道阶段名称:'$graphLookup'”

Posted

技术标签:

【中文标题】Spring Data Mongo Embedded 测试失败并出现“无法识别的管道阶段名称:\'$graphLookup\'”【英文标题】:Spring Data Mongo Embedded test fails with "Unrecognized pipeline stage name: '$graphLookup'"Spring Data Mongo Embedded 测试失败并出现“无法识别的管道阶段名称:'$graphLookup'” 【发布时间】:2019-08-01 02:49:13 【问题描述】:

我使用 Spring Data 和嵌入式 Mongo 的测试失败并显示“无法识别的管道阶段名称:'$graphLookup'”,这表明 MongoDB 的版本是

-- maven 日志--

[mongod output] 2019-03-10T17:54:01.339+0000 I CONTROL  [initandlisten] db version v3.6.5
[mongod output] 2019-03-10T17:54:01.339+0000 I CONTROL  [initandlisten] git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
[mongod output] 2019-03-10T17:54:01.339+0000 I CONTROL  [initandlisten] allocator: tcmalloc
[mongod output] 2019-03-10T17:54:01.339+0000 I CONTROL  [initandlisten] modules: none
[mongod output] 2019-03-10T17:54:01.339+0000 I CONTROL  [initandlisten] build environment:
[mongod output] 2019-03-10T17:54:01.339+0000 I CONTROL  [initandlisten]     distarch: x86_64
[mongod output] 2019-03-10T17:54:01.339+0000 I CONTROL  [initandlisten]     target_arch: x86_64
[mongod output] 2019-03-10T17:54:01.339+0000 I CONTROL  [initandlisten] options:  net:  bindIp: "127.0.0.1", port: 27018 , security:  authorization: "disabled" , storage:  dbPath: "/tmp/embedmongo-db-1cde0ab0-bb3d-4256-8e4c-19401c64337d", journal:  enabled: false , mmapv1:  preallocDataFiles: false, smallFiles: true , syncPeriodSecs: 0.0  
[mongod output] 2019-03-10T17:54:01.339+0000 I STORAGE  [initandlisten] 

...

[mongod output] 2019-03-10T17:54:02.044+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.6

...

rg.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 16436: 'Unrecognized pipeline stage name: '$graphLookup'' on server localhost:43425. The full response is  "ok" : 0.0, "errmsg" : "Unrecognized pipeline stage name: '$graphLookup'", "code" : 16436 ; nested exception is com.mongodb.MongoCommandException: Command failed with error 16436: 'Unrecognized pipeline stage name: '$graphLookup'' on server localhost:43425. The full response is  "ok" : 0.0, "errmsg" : "Unrecognized pipeline stage name: '$graphLookup'", "code" : 16436 
        at team.teamservice.web.team.TeamRepositoryTest.findAncestors(TeamRepositoryTest.java:121)
Caused by: com.mongodb.MongoCommandException: Command failed with error 16436: 'Unrecognized pipeline stage name: '$graphLookup'' on server localhost:43425. The full response is  "ok" : 0.0, "errmsg" : "Unrecognized pipeline stage name: '$graphLookup'", "code" : 16436 
        at team.teamservice.web.team.TeamRepositoryTest.findAncestors(TeamRepositoryTest.java:121)

-- 测试设置--

  @Before
    public void setUp() throws IOException
        

        String ip = "localhost";
        int port = 27018;

        IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.V3_6)
                .net(new Net(ip, port, Network.localhostIsIPv6()))
                .build();

        MongodStarter starter = MongodStarter.getDefaultInstance();
        mongodExecutable = starter.prepare(mongodConfig);
        mongodExecutable.start();

-- 仓库代码--

@Override
public List<Team> findAncestors(String id)
    

    TypedAggregation<Team> agg = Aggregation.newAggregation(Team.class,
            match(Criteria.where("id").is(id)),
            Aggregation.graphLookup("team")
                    .startWith("id")
                    .connectFrom("parentId")
                    .connectTo("id")
                    .as("ancestors"));

    AggregationResults<Team> result = mongoTemplate.aggregate(agg, Team.class);

    List<Team> teams = result.getMappedResults();

    return teams;
    

-- mvn 依赖:树--

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------< team:web >------------------------------
[INFO] Building web 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ web ---
[INFO] team:web:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.0.8.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.0.8.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.8.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.0.8.RELEASE:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.8:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.8:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.8:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.8.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.37:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.37:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.37:compile
[INFO] |  +- org.hibernate.validator:hibernate-validator:jar:6.0.14.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] |  +- org.springframework:spring-web:jar:5.0.12.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:5.0.12.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.0.12.RELEASE:compile
[INFO] |     +- org.springframework:spring-aop:jar:5.0.12.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:5.0.12.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-mail:jar:2.0.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-context-support:jar:5.0.12.RELEASE:compile
[INFO] |  \- com.sun.mail:javax.mail:jar:1.6.2:compile
[INFO] |     \- javax.activation:activation:jar:1.1:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.0.8.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:2.0.8.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.0.8.RELEASE:test
[INFO] |  +- junit:junit:jar:4.12:test
[INFO] |  +- org.assertj:assertj-core:jar:3.9.1:test
[INFO] |  +- org.mockito:mockito-core:jar:2.15.0:test
[INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.7.11:test
[INFO] |  |  +- net.bytebuddy:byte-buddy-agent:jar:1.7.11:test
[INFO] |  |  \- org.objenesis:objenesis:jar:2.6:test
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] |  +- org.springframework:spring-core:jar:5.0.12.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-test:jar:5.0.12.RELEASE:test
[INFO] |  \- org.xmlunit:xmlunit-core:jar:2.5.1:test
[INFO] +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO] |  +- net.minidev:json-smart:jar:2.3:test
[INFO] |  |  \- net.minidev:accessors-smart:jar:1.2:test
[INFO] |  |     \- org.ow2.asm:asm:jar:5.0.4:test
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-mongodb:jar:2.0.8.RELEASE:compile
[INFO] |  +- org.mongodb:mongodb-driver:jar:3.6.4:compile
[INFO] |  |  +- org.mongodb:bson:jar:3.6.4:compile
[INFO] |  |  \- org.mongodb:mongodb-driver-core:jar:3.6.4:compile
[INFO] |  \- org.springframework.data:spring-data-mongodb:jar:2.0.13.RELEASE:compile
[INFO] |     +- org.springframework:spring-tx:jar:5.0.12.RELEASE:compile
[INFO] |     \- org.springframework.data:spring-data-commons:jar:2.0.13.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-rest:jar:2.0.8.RELEASE:compile
[INFO] |  \- org.springframework.data:spring-data-rest-webmvc:jar:3.0.13.RELEASE:compile
[INFO] |     +- org.springframework.data:spring-data-rest-core:jar:3.0.13.RELEASE:compile
[INFO] |     |  +- org.springframework.hateoas:spring-hateoas:jar:0.25.0.RELEASE:compile
[INFO] |     |  +- org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile
[INFO] |     |  \- org.atteo:evo-inflector:jar:1.2.2:compile
[INFO] |     \- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:2.0.8.RELEASE:compile
[INFO] |  +- org.thymeleaf:thymeleaf-spring5:jar:3.0.11.RELEASE:compile
[INFO] |  \- org.thymeleaf.extras:thymeleaf-extras-java8time:jar:3.0.2.RELEASE:compile
[INFO] +- nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar:2.3.0:compile
[INFO] |  +- nz.net.ultraq.thymeleaf:thymeleaf-expression-processor:jar:1.1.3:compile
[INFO] |  +- org.codehaus.groovy:groovy:jar:2.4.16:compile
[INFO] |  \- org.thymeleaf:thymeleaf:jar:3.0.11.RELEASE:compile
[INFO] |     +- ognl:ognl:jar:3.1.12:compile
[INFO] |     |  \- org.javassist:javassist:jar:3.20.0-GA:compile
[INFO] |     +- org.attoparser:attoparser:jar:2.0.5.RELEASE:compile
[INFO] |     \- org.unbescape:unbescape:jar:1.1.6.RELEASE:compile
[INFO] +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:2.0.8.RELEASE:compile (optional)
[INFO] |  +- org.springframework.boot:spring-boot:jar:2.0.8.RELEASE:compile
[INFO] |  \- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.8.RELEASE:compile
[INFO] +- com.google.visualization:visualization-datasource:jar:1.1.1:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.4:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- com.google.guava:guava:jar:r07:compile
[INFO] |  +- com.ibm.icu:icu4j:jar:4.0.1:compile
[INFO] |  \- net.sf.opencsv:opencsv:jar:1.8:compile
[INFO] \- de.flapdoodle.embed:de.flapdoodle.embed.mongo:jar:2.2.0:test
[INFO]    \- de.flapdoodle.embed:de.flapdoodle.embed.process:jar:2.1.2:test
[INFO]       +- org.apache.commons:commons-lang3:jar:3.7:test
[INFO]       +- net.java.dev.jna:jna:jar:4.5.2:test
[INFO]       +- net.java.dev.jna:jna-platform:jar:4.5.2:test
[INFO]       \- org.apache.commons:commons-compress:jar:1.18:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.749 s
[INFO] Finished at: 2019-03-10T18:04:13Z
[INFO] ------------------------------------------------------------------------

【问题讨论】:

【参考方案1】:

我想通了。我将 Spring Boot 自动配置与手动非 Spring 配置混淆了,最终导致两个 mongodb 实例在两个不同的端口上运行不同的版本。

只要您在测试中有@DataMongoTest 注解,嵌入式 mongodb 就会自动配置。从您的测试设置中删除嵌入式 mongodb 配置,否则您最终会在测试期间运行并行 mongo 版本。

要更改版本,您只需设置一些应用程序属性:

spring.mongodb.embedded.version=3.6.4
spring.mongodb.embedded.features=no_http_interface_arg,only_with_ssl

请参阅https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/223 和https://github.com/spring-projects/spring-boot/issues/12575 了解更多详情。

【讨论】:

此解决方案也适用于版本 4.0.3。我只需要将 only_with_ssl 添加到功能列表中。

以上是关于Spring Data Mongo Embedded 测试失败并出现“无法识别的管道阶段名称:'$graphLookup'”的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 mongo 搜索集合并返回子文档列表(Spring-data-mongo)

使用 Spring data mongo 和 Spring data elasticsearch 时如何建模?

使用 mongoTemplate 在 spring-data-mongo Java 中进行 Mongo 聚合查询

spring-data-mongo - 可选查询参数?

spring-data-mongo的MongoTemplate开发

spring data mongo使用小记