skip to Main Content

I have a class

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

I want to update class to:

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

Don’t care about code convention. I want to write a migration to convert data from boolean to long in mongodb.

I try to create a new class

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

but I think that is not a good solution.

2

Answers


  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);
                    }
                }
            }
        }
    }
    
    Login or Signup to reply.
  2. As I think that you are looking for a a way to manage this migration with Mongock, I recommend to perform the actual MongoDB migration that @wpdnqd is providing inside a ChangeUnit.

    Something like this

    
    @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);
                    }
                }
           
        }
    }
    
    

    For more example, take a look to this repo and the official documentation

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search