Lazy migration of documents

69 views
Skip to first unread message

Mickael Bailly

unread,
Feb 5, 2014, 6:01:15 AM2/5/14
to mongoo...@googlegroups.com
  Hello community,

I look at the right way to handle mongoose schema migrations. I read lots of things, including https://groups.google.com/forum/#!topic/mongoose-orm/6twzh-4d6q8 , and saw Aaron response. I'm really seduced by the lazy migration way of doing things, but unfortunately the middleware doesn't seem to provide enough hooks. Let's take an example. I got a schema like:

{
   title: String,
   updates: [Date],
   schemaVersion: 1
}

and then we decide to add to the updates the id of the user. So my version 2 is:

{
   title: String,
   updates: [{date: Date, author: ObjectId}],
   schemaVersion: 2
}

If I use the schema.pre('init') hook (which seems the first available hook in the lifecycle of the Document), the mongoose document has already been hydrated. So a document {title: 'Hello world', updates: [234234234], schemaVersion: 1} will be hydrated as: {title: 'Hello world, updates:[], schemaVersion: 1}.
I saw that in the pre('init') hook, the 2nd argument is the mongodb object. So I can cheat and lookup into it to correctly set the properties, according to the new schema. However, using this strategy, I should code migrations from any schema version to any other one, so if I have schema versions 1, 2 and 3, I should have migration from 1=>2, 1=>3, and 2=>3... That's complex.

I could have missed something, but for now it seems to me that the best way to handle those lazy migrations would be to have a hook before mongoose hydratation, that takes the mongodb document and returns the perhaps modfied document. Is this kind of thing stupid, or cool ?

Thanks,

Mickael

Aaron Heckmann

unread,
Feb 17, 2014, 11:03:55 AM2/17/14
to mongoo...@googlegroups.com
Neither stupid or cool are how I'd classify. 

This is a good solution for folks with data sets too large to run a migration script all at once. Complex yes. So if you can get by with a simple migration and avoid this I'd probably go with that.
--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--

Reply all
Reply to author
Forward
0 new messages