Zhen Liu
Zhen Liu

Reputation: 7932

Mongoose / mongoDB drop unique index constraint

I created a unique index using Mongoose sometime ago in my database.

Model.index({name: 1, location: 1}, {unique: true});

Now due to some coding changes, I would like to drop the uniqueness on the index. Is there a way to just drop the uniqueness on the index?

The only thing I can come up with is to just do

Model.dropIndex({name: 1, location: 1});
Model.index({name: 1, location: 1});

Is this correct? Or is there less a confusing way to write the code..

Upvotes: 4

Views: 4466

Answers (3)

user2960993
user2960993

Reputation: 711

If you use a UI tool like RoboMongo, you can drop it from its UI

Upvotes: 0

enRaiser
enRaiser

Reputation: 2636

Yes that is correct way of dropping. You can either do it from mongo client.

or from node server like this

Model( { {name: 1, location: 1} }, function(err){
     if(err){
         res.send("error");
    }
    else{
        res.send("success");
    }
});

Upvotes: 0

DAXaholic
DAXaholic

Reputation: 35358

Yes as stated in the docs:

To modify an existing index, you need to drop and recreate the index with the exception of m TTL indexes.

So your code is correct for the time being. Maybe in the future they add the possibility to change it in place.

Upvotes: 4

Related Questions