0

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?

2
  • Why not build a script using Javascript and run directly on server? Commented Aug 12, 2013 at 12:58
  • _class - defined from config file, and diffident by customer. Commented Aug 12, 2013 at 13:06

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

but how I can make my script run only once while deploying?
It depends on you, at what time you want to migrate database or update data. If you want to run the script one time, create an independent script from project, and before running project firstly run the script for migrating database or update data in database.
0

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) {

        }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.