Prateek Chachra
Prateek Chachra

Reputation: 190

How to modify existing schema in mongoose?

I am using mongoose for my application. I had a few simple models I was using for my application like Posts, Users and Profile. I now have changed the schema a bit, but I am getting this error everytime I try to run the program

OverwriteModelError: Cannot overwrite `post` model once compiled.
[0]     at new OverwriteModelError (C:\Users\prate\PRACTISE CODE\Evaluate\node_modules\mongoose\lib\error\overwriteModel.js:20:11)
[0]     at Mongoose.model (C:\Users\prate\PRACTISE CODE\Evaluate\node_modules\mongoose\lib\index.js:472:13)
    at Object.<anonymous> (C:\Users\prate\PRACTISE CODE\Evaluate\models\Post.js:45:34)
[0]     at Module._compile (internal/modules/cjs/loader.js:701:30)
[0]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
[0]     at Module.load (internal/modules/cjs/loader.js:600:32)
[0]     at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
[0]     at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
[0]     at require (internal/modules/cjs/helpers.js:22:18)
[0]     at Object.<anonymous> (C:\Users\prate\PRACTISE CODE\Evaluate\routes\api\posts.js:8:14)
[0]     at Module._compile (internal/modules/cjs/loader.js:701:30)
[0]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
[0]     at Module.load (internal/modules/cjs/loader.js:600:32)
[0]     at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
[0]     at Function.Module._load (internal/modules/cjs/loader.js:531:3)

I've tried searching on StackOverflow about the problem, there was one solution and that was to set 'strict: false', but I don't want my user model to have garbage values with it, I just want to redefine the structure.

Previously my Post model had these fields:

const PostSchema = new Schema({
    user: {
        type: Schema.Types.ObjectId,
        ref: 'users'
    },
    text: {
        type: String,
        required: true,
    },
    speechBody: {
        type: String

    },
    youtubeLink: {
        type: String
},
comments : [ {
    text: {
        type: String
}
}

],
});

Now I've defined a new schema called CommentSchema which has likes and unlikes and many other features.

Hence now my model looks like this:

const PostSchema = new Schema({
    user: {
        type: Schema.Types.ObjectId,
        ref: 'users'
    },
    text: {
        type: String,
        required: true,
    },
    speechBody: {
        type: String

    },
    youtubeLink: {
        type: String
},
comments : [commentSchema],
});

Where CommentSchema looks like this:

const CommentSchema = new Schema({
    user: {
        type: Schema.Types.ObjectId,
        ref: 'users'
    },
    text: {
        type: String,
        required: true
    },
    name : {
        type: String,
    },
    avatar: {
        type: String
    },
    date: {
        type: Date,
        default: Date.now
    }
});

Can anyone guide me how to modify the schema in mongoose? I don't want to use 'strict: false', also I don't want the data already existing in my mongodb to go away. I just want the same old data with this new structure.

Upvotes: 1

Views: 313

Answers (1)

Intellidroid
Intellidroid

Reputation: 1055

Have you tried creating a ref to the commentSchema instead of using a sub document? Something like this?

comments:[{ type: Schema.Types.ObjectId, ref: 'Comments' }]

This way you can have another model and schema and not overwrite your existing data.

Upvotes: 1

Related Questions