使用 Mongock 和 Spring Boot 进行数据库迁移

Database Migration with Mongock and Spring Boot

提问人:hungtam 提问时间:11/16/2023 更新时间:11/18/2023 访问量:35

问:

我有一个班级

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document("animal")
public class Animal {
    private String id;
    private boolean isBirthYear;
}

我想将类更新为:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document("animal")
public class Animal {
    private String id;
    private long isBirthYear;
}

不要在乎代码约定。我想在mongodb中编写一个迁移以将数据从布尔转换为long。

我尝试创建一个新类

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document("animal")
public class AnimalV2 {
    private String id;
    private long isBirthYear;
}

但我认为这不是一个好的解决方案。

spring-boot 数据库-迁移 mongock

评论


答:

-1赞 user22919420 11/16/2023 #1
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.bson.Document;

public class AnimalMigration {

    public static void main(String[] args) {
        try (var mongoClient = MongoClients.create("your-mongodb-connection-string")) {
            var database = mongoClient.getDatabase("your-database-name");
            var collection = database.getCollection("animal");

            // Get all documents in the collection
            try (var cursor = collection.find().iterator()) {
                while (cursor.hasNext()) {
                    var document = cursor.next();

                    // Convert boolean to long for the isBirthYear field
                    if (document.containsKey("isBirthYear")) {
                        var isBirthYear = document.getBoolean("isBirthYear");
                        document.put("isBirthYear", isBirthYear ? 1L : 0L);
                    }

                    // Save the updated document back to the collection
                    collection.replaceOne(new Document("_id", document.getObjectId("_id")), document);
                }
            }
        }
    }
}
1赞 Mongock team 11/18/2023 #2

由于我认为您正在寻找一种使用 Mongock 管理此迁移的方法,因此我建议执行@wpdnqd在 ChangeUnit 中提供的实际 MongoDB 迁移。

像这样的东西


@ChangeUnit(id = "udpate-animal-birthday", order = "1")
class ACreateCollection {

    @Execution
    fun execution(mongoClient: MongoClient) {
           var database = mongoClient.getDatabase("your-database-name");
            var collection = database.getCollection("animal");

            // Get all documents in the collection
            try (var cursor = collection.find().iterator()) {
                while (cursor.hasNext()) {
                    var document = cursor.next();

                    // Convert boolean to long for the isBirthYear field
                    if (document.containsKey("isBirthYear")) {
                        var isBirthYear = document.getBoolean("isBirthYear");
                        document.put("isBirthYear", isBirthYear ? 1L : 0L);
                    }

                    // Save the updated document back to the collection
                    collection.replaceOne(new Document("_id", document.getObjectId("_id")), document);
                }
            }
       
    }
}

有关更多示例,请查看此存储库官方文档