0

I have a mongoose schema,

const loggerSchema = mongoose.Schema(
    {
        // Other ...
        cluster: {
            type: String,
            required: true,
        },
        // ...
    }
)

It currently filled with lots of data. I want to modify the cluster property to be as follows

const loggerSchema = mongoose.Schema(
    {
        // Other ...
        cluster: {
            type: String,
            required: true,
            unique:true
        },
        // ...
    }
)

How do I achieve this without losing any of my data (in DB)? Provided that all existing data contain unique values on cluster attribute.

daniel ernest
  • 345
  • 3
  • 9
  • maybe dup of https://stackoverflow.com/questions/7617002/dealing-with-schema-changes-in-mongoose – Ofir Bi Sep 20 '21 at 11:28

1 Answers1

0

If you are sure there are no duplicate values of cluster you can change your schema immediately as you mentioned (if there are dups it will fail to create the unique index on cluster).
Read more about unique option here.

Ofir Bi
  • 236
  • 1
  • 2