1

I have a very basic question, I tried google but I cant really find anything.

I'm trying out mongoose js and I was wondering how you go about updating a live application.

Say for example I have defined a schema in my node.js application, and inside my app.js file somewhere:

...
var LibrarySchema = new mongoose.Schema({
    address: {type: String, required: true},
    books: [{
        title: {type: String, required: true}
    }]
});

var libraryModel = mongoose.model('Library', LibrarySchema);
...

Now I've run the server on some AWS instance somewhere in the cloud. Users are on there, using the site. At some point I decide that every book should have an ISBN number, so I need to update the Schema.

Now what are my options? Do I edit the app.js file on the server then restart the node js app? Or is there a way to edit schemas while in production in a way that minimizes downtime?

Thanks!!

JohnE
  • 155
  • 1
  • 1
  • 7
  • Duplicate: http://stackoverflow.com/questions/7617002/dealing-with-schema-changes-in-mongoose – kentcdodds Jul 10 '13 at 01:10
  • Thats not really a duplicate. I am asking about updating the schema in a LIVE deployment. Not about updating the schema in general :) – JohnE Jul 11 '13 at 04:15

2 Answers2

0

The mongodb is entirely separate from your app, so as long as your not using this

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test'); 

you can update the database using a separate app, and as long as your main app knows how to meaningfully use the new data, then you shouldn't have to restart it or change anything.

Loourr
  • 4,995
  • 10
  • 44
  • 68
0

You can change the schema in Mongoose and reload the application. If you have more than one instance of your server, you can change it step by step without interrupting your users. But you should be careful, how to deal with new schema. The old instance knows nothing about new items, so it is OK, but the new one should check new items and deal with situation if they are not present. If the change is only small it is good idea to fulfil missing items in the database with reasonable defaults after migration and if the change is more radical it is possible to use simultaneously different versions of your schema and have version identificator in your schema.

ivoszz
  • 4,370
  • 2
  • 27
  • 27