mongodb - Spring Data Mongo 嵌入式测试失败,错误为 "Unrecognized pipeline stage name: ' $graphLookup'"

标签 mongodb spring-data spring-data-mongodb

我使用 Spring Data 和嵌入式 Mongo 的测试失败,并显示“无法识别的管道阶段名称:'$graphLookup'”,这表明 MongoDB 的版本小于 3.4,但是我在设置测试时指定了 v3.6,我可以从日志表明 featureCompatibility 设置正确,并且 mongo db 驱动程序在版本 3.6.4 上看起来正常。当我针对 MongoDB 的独立 3.6 实例运行我的代码(不是测试)时,它按预期工作 - 只是嵌入的 fladoodle mongo 的测试失败了。请问有什么指点吗?

-- 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] ------------------------------------------------------------------------

最佳答案

我明白了这一点。我混淆了 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/223https://github.com/spring-projects/spring-boot/issues/12575了解更多详情。

关于mongodb - Spring Data Mongo 嵌入式测试失败,错误为 "Unrecognized pipeline stage name: ' $graphLookup'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090852/

相关文章:

mongodb - "invalid hexadecimal representation"用于 spring mongo 数据存储库接口(interface)

java - spring-data-mongodb 实体实现接口(interface)创建错误的集合

python - Pymongo 或 Mongodb 将两个相等的 python 字典视为不同的对象。我可以强制他们受到同样的对待吗?

node.js - 如何在mongodb查询中使用变量作为字段值?

node.js - 分别获取 MongoDB 集合后加入它们

java - 如何使用 Spring Data JDBC 处理复合键

java - spring-data-jpa 中的交叉连接

java - 使用 Spring MongoOperations 根据查询获取所有文档

java - 如何在 Spring Data Mongodb 中进行 GroupBy 而不进行聚合?

java - MongoDB 子文档中的增量值