I use mongodb + node.js + mongoose.js ORM backend.
Let say I I have some nested array of object without _id field
mongoose.Schema({
nested: [{
_id: false, prop: 'string'
}]
})
And then I want to ad _id field to all nested objectds, so the mongoose schema would be
mongoose.Schema({
nested: [{
prop: 'string'
}]
})
Then I should run some script to modify production DB, right? What is the best way to handle such change? Which tool (or approach) is best to use to implement the change?
One of the significant advantages of schema-less databases is that you don’t have to update the entire database with new schema layouts. If some of the documents in the DB don’t have particular information, then your code can do the appropriate thing instead, or elect to now do anything with that record.
Another option is to lazily update the documents as required – only when they are looked at again. In this instance, you might elect to have a per-record/document version flag – which initially may not even appear (and thus signify a ‘version 0’). Even that is optional though. Instead, your database access code looks for data it requires, and if it does not exist, because it is new information, added after a code update, then it would fill in the results to the best of its ability.
For your example, converting an
_id:falseinto a standardMongoIdfield, when the code is read (or written back after an update), and the_id:falseis currently set, then make the change and write it only when it is absolutely required.