0

我有一个文档集合,如下所示:

[
    {
        "firstName" : "Foo",
        "phone" : "+1 111 1111",
        "car": "Toyota"
    },
    {
        "firstName" : "Bar",
        "phone" : "+2 222 2222",
        "car": "Honda"
    }
]

我想为这个集合“Persons”中的每个文档添加一个新字段,我可以在控制台上执行语法,但是由于我需要以某种方式存储更改,所以我们使用的是 Mongock。而且我找不到执行updatetrough的方法MongockTemplate。文档是静态的{ "key" : "value" },我什至不需要变量来传递它们,最终文档应该如下所示:

[
    {
        "firstName" : "Foo",
        "phone" : "+1 111 1111",
        "car": "Toyota",
        "species" : "human"
    },
    {
        "firstName" : "Bar",
        "phone" : "+2 222 2222",
        "car": "Honda",
        "species" : "human"
    }
]

我不知道如何在里面执行如下查询MongockTemplate

db.getCollection('Persons').update(
  {},
  { $set: {"species": "human"} },
  false,
  true
)

Here's the StackTrace:

```java
22:18:48.237 [main] ERROR io.changock.runner.core.ChangockBase - Error in changock process. ABORTED MIGRATION
io.changock.migration.api.exception.ChangockException: Error in method[UpdateAddTypeField.updateAddTypeField] : 'com.mongodb.client.result.UpdateResult org.springframework.data.mongodb.core.MongoTemplate.updateMulti(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.String)'
    at io.changock.runner.core.MigrationExecutor.processExceptionOnChangeSetExecution(MigrationExecutor.java:195)
    at io.changock.runner.core.MigrationExecutor.processSingleChangeSet(MigrationExecutor.java:102)
    at io.changock.runner.core.MigrationExecutor.lambda$processSingleChangeLog$2(MigrationExecutor.java:94)
    at io.changock.runner.core.MigrationExecutor.executeInTransactionIfStrategyOrUsualIfNot(MigrationExecutor.java:80)
    at io.changock.runner.core.MigrationExecutor.processSingleChangeLog(MigrationExecutor.java:94)
    at io.changock.runner.core.MigrationExecutor.lambda$processAllChangeLogs$1(MigrationExecutor.java:88)
    at io.changock.runner.core.MigrationExecutor.executeInTransactionIfStrategyOrUsualIfNot(MigrationExecutor.java:80)
    at io.changock.runner.core.MigrationExecutor.processAllChangeLogs(MigrationExecutor.java:88)
    at io.changock.runner.core.MigrationExecutor.lambda$executeMigration$0(MigrationExecutor.java:69)
    at io.changock.runner.core.MigrationExecutor.executeInTransactionIfStrategyOrUsualIfNot(MigrationExecutor.java:80)
    at io.changock.runner.core.MigrationExecutor.executeMigration(MigrationExecutor.java:69)
    at io.changock.runner.spring.v5.core.SpringMigrationExecutor.executeMigration(SpringMigrationExecutor.java:38)
    at io.changock.runner.core.ChangockBase.execute(ChangockBase.java:44)
    at io.changock.runner.spring.v5.ChangockSpringBuilderBase$ChangockSpringApplicationRunner.run(ChangockSpringBuilderBase.java:110)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:786)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:776)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
    at com.hiden.subhiden.project.subhidenprojecterviceApplication.main(subhidenprojecterviceApplication.java:22)
Caused by: java.lang.reflect.InvocationTargetException: null
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at io.changock.runner.core.MigrationExecutor.executeChangeSetMethod(MigrationExecutor.java:174)
    at io.changock.runner.core.MigrationExecutor.executeAndLogChangeSet(MigrationExecutor.java:127)
    at io.changock.runner.spring.v5.core.SpringMigrationExecutor.executeAndLogChangeSet(SpringMigrationExecutor.java:44)
    at io.changock.runner.core.MigrationExecutor.processSingleChangeSet(MigrationExecutor.java:100)
    ... 16 common frames omitted
Caused by: java.lang.NoSuchMethodError: 'com.mongodb.client.result.UpdateResult org.springframework.data.mongodb.core.MongoTemplate.updateMulti(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.String)'
    at com.github.cloudyrock.mongock.driver.mongodb.springdata.v2.decorator.impl.MongockTemplate.lambda$updateMulti$53(MongockTemplate.java:383)
    at io.changock.driver.api.lock.guard.invoker.LockGuardInvokerImpl.invoke(LockGuardInvokerImpl.java:18)
    at com.github.cloudyrock.mongock.driver.mongodb.springdata.v2.decorator.impl.MongockTemplate.updateMulti(MongockTemplate.java:383)
    at com.hiden.subhiden.project.dbchangelogs.UpdateAddTypeField.updateAddTypeField(UpdateAddTypeField.java:32)
    ... 24 common frames omitted
22:18:48.315 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    com.github.cloudyrock.mongock.driver.mongodb.springdata.v2.decorator.impl.MongockTemplate.lambda$updateMulti$53(MongockTemplate.java:383)

The following method did not exist:

    'com.mongodb.client.result.UpdateResult org.springframework.data.mongodb.core.MongoTemplate.updateMulti(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.String)'

The method's class, org.springframework.data.mongodb.core.MongoTemplate, is available from the following locations:

    jar:file:/home/johnnes/.m2/repository/org/springframework/data/spring-data-mongodb/3.0.5.RELEASE/spring-data-mongodb-3.0.5.RELEASE.jar!/org/springframework/data/mongodb/core/MongoTemplate.class

The class hierarchy was loaded from the following locations:

    org.springframework.data.mongodb.core.MongoTemplate: file:/home/johnnes/.m2/repository/org/springframework/data/spring-data-mongodb/3.0.5.RELEASE/spring-data-mongodb-3.0.5.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.data.mongodb.core.MongoTemplate


Process finished with exit code 1
4

3 回答 3

1

任何你可以用普通的 MongoTemplate 做的事情,你也可以用 MongockTemplate 做。

在这种情况下,您可以调整 UpdateOptions 和其他一些东西,但它可以像

       Document updater = new Document()
          .append("$set", new Document().append("species", "human"));
        
       mongockTemplate.getCollection("Persons")
           .updateMany(new Document(), updater);
于 2021-02-26T23:06:40.927 回答
1

虽然之前的答案仍然有效,但在您发布错误日志后,我想我发现了问题。您正在使用不兼容的 MongoDB Spring Data 和 Mongock Driver 版本。一方面,您使用的是 MongoDB Spring Data 3.0.5.RELEASE,另一方面,您使用的是 Mongock 驱动程序mongodb-spring-data-v2,该驱动程序旨在与 Spring Data 2 一起使用。您应该mongodb-spring-data-v3使用.

更多信息,请查看官方文档

于 2021-03-12T09:12:31.110 回答
0

因此,经过多次尝试,我找到了以下解决方案:通过 MongockTemplate 获取 MongoTemplate Impl 的实例并使用它而不是 MongockTemplate。我仍然不知道它为什么起作用。如果我弄清楚了,我会更新这个线程。

@ChangeSet(order = "1", id = "updateData", author = "J. Souza")
  public void updateCollectionAddSpecieField(MongockTemplate mongockTemplate) {

    MongoTemplate template = mongockTemplate.getImpl();

    Query query = new Query();
    query.addCriteria(where(SPECIES_FIELD).exists(false));
    Update update = new Update();
    update.set(SPECIES, HUMAN);
    UpdateResult updateResult = template
        .updateMulti(query, update, "Collection-Name");

    log.info("Matched {} | Modified {}",
        updateResult.getMatchedCount(), updateResult.getModifiedCount());
  }
于 2021-03-01T12:25:32.597 回答