Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose Changing Schema Format

We're rapidly developing an application that's using Mongoose, and our schema's are changing often. I can't seem to figure out the proper way to update a schema for existing documents, without blowing them away and completely re-recreating them from scratch.

I came across http://mongoosejs.com/docs/api.html#schema_Schema-add, which looks to be right. There's little to no documentation on how to actually implement this, making it very hard for someone who is new to MongoDB.

I simply want to add a new field called enabled. My schema definition is:

var sweepstakesSchema = new Schema({     client_id: {         type: Schema.Types.ObjectId,         ref: 'Client',         index: true     },     name: {         type: String,         default: 'Sweepstakes',     },     design: {         images: {             type: [],             default: []         },         elements: {             type: [],             default: []         }     },     enabled: {         type: Boolean,         default: false     },     schedule: {         start: {             type: Date,              default: Date.now         },         end: {             type: Date,             default: Date.now         }     },     submissions: {         type: Number,         default: 0     } }); 
like image 499
Nick Parsons Avatar asked Jan 11 '13 22:01

Nick Parsons


People also ask

Can Mongoose schema be changed?

There's nothing built into Mongoose regarding migrating existing documents to comply with a schema change. You need to do that in your own code, as needed.

Can we change schema in MongoDB?

Collections in MongoDB do not have a fixed schema, or requirement for all documents in a collection to have the same schema. You can definitely add or remove fields, change field types, or update JSON Schema validation without recreating a collection.

What is Mongoose schema type?

Mongoose Schematype is a configuration for the Mongoose model. Before creating a model, we always need to create a Schema. The SchemaType specifies what type of object is required at the given path. If the object doesn't match, it throws an error.

What does $Set do in Mongoose?

The $set operator replaces the value of a field with the specified value.


Video Answer


2 Answers

Considering your Mongoose model name as sweepstakesModel, this code would add enabled field with boolean value false to all the pre-existing documents in your collection:

db.sweepstakesModel.find( { enabled : { $exists : false } } ).forEach(     function (doc) {         doc.enabled = false;         db.sweepstakesModel.save(doc);     } ) 
like image 181
Vickar Avatar answered Sep 18 '22 18:09

Vickar


There's nothing built into Mongoose regarding migrating existing documents to comply with a schema change. You need to do that in your own code, as needed. In a case like the new enabled field, it's probably cleanest to write your code so that it treats a missing enabled field as if it was set to false so you don't have to touch the existing docs.

As far as the schema change itself, you just update your Schema definition as you've shown, but changes like new fields with default values will only affect new documents going forward.

like image 39
JohnnyHK Avatar answered Sep 20 '22 18:09

JohnnyHK