Shen Lance Shen Lance - 1 year ago 170
JSON Question

How to import sails.js disk.db to mongodb

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

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": ""
"user": [
"name": "user0001",
"email": "",
"role": [
"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 (
) to mongodb while maintaing my json data format or collections? Or I need to create my collections on mongodb manually and import each collection?

Answer Source

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

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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download