Lugaru
Lugaru

Reputation: 1460

Deploy script once for migrating data in MongoDB

Situation: have two mongo DBs for project. Each document in DB contains _id and _class fields. After updates in a project I need change _class field in all documents. That is some kind of migration.

I'm thinking about implementing this migration script into WEB part of the project, while deploying script should update all documents.

Info:

1) Script should run only once during deploying.

2) I'm using Spring data framework

Question: I have no clue how to run migration script only once. Is it possible to do using Spring framework? And what is most efficient way to do it?

Upvotes: 0

Views: 1332

Answers (2)

Lugaru
Lugaru

Reputation: 1460

After some time of experiments found way how to run my code for updating DB once, while project deploying.

Simply the idea to use @Component annotation from spring framework.

Class looks looks this:

@Component
public class MongoUpdate {
    /**
     * Constructor.
     */
    public MongoUpdate() {

        try {
                // Code to update mongo here
            }

        } catch (MongoException ex) {

        } catch (UnknownHostException ex) {

        }

    }
}

Upvotes: 0

Harmeet Singh Taara
Harmeet Singh Taara

Reputation: 6611

In MongoDb, there is update() function that is used to Update the data in document. This is overloaded function , you pass the criteria for update all values or some selected values according to criteria.

Upvotes: 2

Related Questions