3

So I have a nodejs app that is using sailsjs web framework. While Im developing, I have the adapter set to sails-disk. Sample data from disk.db is like

{
    "data": {
        "authentication": [
            {
                "user_id": 1,
                "username": "user1",
                "encrypted_password": "password here",
                "createdAt": "2014-05-12T07:40:24.901Z",
                "updatedAt": "2014-08-18T19:37:22.851Z",
                "id": 1,
                "email": "[email protected]"
            }
        ],
        "user": [
            {
                "name": "user0001",
                "email": "[email protected]",
                "role": [
                    "admin"
                ],
                "phone": [
                    {}
                ],
                "status": "Active",
                "createdAt": "2014-05-12T07:48:11.028Z",
                "updatedAt": "2014-05-24T08:12:41.646Z",
                "id": 1,
                "username": "user1"
            }
        ]
    }
}

This works perfectly on my local machine. Now, I'm ready to deploy it on production and use mongodb as my database. I have all the connections setup and I can successfully connect on my db.

My question is: Is there a way that I can import my local disk database (disk.db) to mongodb while maintaing my json data format or collections? Or I need to create my collections on mongodb manually and import each collection?

2
  • Are you concerned about migrating actual data records, or just collections? MongoDB will create the collections for you as they are needed, so you don't need to do anything if that's all you want to happen. Commented Aug 21, 2014 at 21:16
  • Yes- the actual data records. Commented Aug 22, 2014 at 2:17

3 Answers 3

4

There's no tool or official way to do this kind of migration with Sails. Depending on how many models you have and how tedious it would be to write a script to migrate things for you, one option is to mirror your models--that is, if you have a User.js, make a new model UserNew.js that looks like:

module.exports = {

    connection: 'mongoDbConnection',
    tableName: 'user',
    attributes: <same as User.js attributes>

}

and then in your config/bootstrap.js do:

User.find().exec(function(err, users) {
    UserNew.create(users).exec(...);
}

to transfer them over.

This would be a one-time thing after which you could drop your original User.js and rename UserNew.js to User.js. Pretty hacky, but again depending on how much data we're talking about, it might be the quickest option!

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

Comments

0

I built on the answer from sgress454 and flipped it around. Instead of a NewUser I created a OldUserLocation.js model.

module.exports = {
    connection: 'localDiskDb',
    tableName: 'user',
    attributes: {
    //same as in User.js
}

}

And in the default model I specified mongo DB.

In the bootstrap function I added this:

User.find().exec(function(err, users){
    if(!err && users.length == 0){
        OldUserLocation.find().exec(function(err, oldUsers) {
            if(!err && oldUsers.length >0){
                User.create(oldUsers).exec(function (err){
                    cb();
                });
            }
            else{
                cb();
            }
        });
    }
    else{
        cb();  
    } 
});

Simply put, if the user model in the mongo DB has no users then I try and import from the localdisk db. If it already has users I assume the import is done already.

Comments

0

Another hacky way I see is :

  • open .tmp/localDiskDb.db
  • copy a an object
  • run a POST query using Postman https://www.getpostman.com/apps
  • set the query to POST
  • set the endpoint URL in the "body" tab choose 'raw', 'JSON' paste the object in the box
  • press send

rince, repeat

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.