Dropping an existing index from MongoDB or mongoose

  •  Sharad Jaiswal
    Posted by Sharad Jaiswal

    Dropping an existing index in MongoDB

    While working with MongoDB and Node js I face an issue. I have created  mobile_no unique in starting and after some time I realized that I don't need my mobile_no to be unique. so I just go to my model and remove the unique: true property from my table schema and rebuild my app after. But strange the changes in schema did not reflect, I will still get the same error, mobile no must be unique. I have read my tutorial and found this code to Drop an existing index from mongo or mongoose using Model.

    Here is a way to drop a unique index from mongoose schema

    'use strict';
    var mongoose = require('mongoose');
    var bcrypt = require('bcrypt');
    
    var DriverSchema = new mongoose.Schema({
    	first_name: {
    		type: String,
    		required: true,
    		trim: true
    	},
    	last_name: {
    		type: String,
    		required: true,
    		trim: true
    	},
    	email: {
    		type: String,
    		required: true,
    		trim: true,
    		unique : true,
    	},
    	emp_id: {
    		type: String,
    		trim: true,
    		default: ''
    	},
    	dob: {
    		type: Date,
    		default: Date.now
    	},
    	address: {
    		type: String,
    		trim: true,
    		default: ''
    	},
    	locality: {
    		type: String,
    		trim: true,
    		default: ''
    	},
    	city: {
    		type: String,
    		trim: true,
    		default: ''
    	},
    	state: {
    		type: String,
    		trim: true,
    		default: ''
    	},
    	zipcode: {
    		type: String,
    		trim: true,
    		default: ''
    	},
    	country_code: {
    		type: String,
    		default: ''
    	},
    	mobile_no: {
    		type: String,
    		trim: true,
    		default: 0,
           	
    	},
    	mobile_verified: {
    		type: Number,
    		default:0
    		
    	},
    	email_verified: {
    		type: Number,
    		default:0
    		
    	},
    	device_id: {
    		type: String,
    		required: true,
    		trim: true,
    		default: ''
    	},
    	gcm_id: {
    		type: String,
    		required: true,
    		trim: true,
    		default: ''
    	},
    	profile_pic: {
    		type: String,
    		trim: true,
    		default: ''
    	},
    	device_type: {
    		type: String,
    		default: 'android'
    	},
    	status: {
    		type: String,
    		default: 'pending'
    	},
            password: {
    		type: String,
    		required: true,
    		
    	},
    	created_at: {
    		type: Date,
    		default: Date.now
    	},
    	updated_at: {
    		type: Date,
    		default: Date.now
    	}
    });
    // hashing password in MongoDB
    DriverSchema.pre('save', function(next) {
        let userValues = this;
    
        bcrypt.hash(userValues.password, 10).then((hash)=> {
            userValues.password = hash; //if there is no error we are going to hash
    		console.log(hash);
            next();
        }).catch((err)=> {
            console.log(err);
        });
    });
    // comparing password in MongoDB
    DriverSchema.methods.comparePassword = function(pwd){
            let uInfo = this;
            return bcrypt.compareSync(pwd, uInfo.password);
    };
    
    var Driver = mongoose.model('Driver', DriverSchema);
    // Dropping an Index in MongoDB
    Driver.collection.dropIndex('mobile_no', function(err, result) {
        if (err) {
            console.log('Error in dropping index!', err);
        }
    });
    module.exports = Driver;
    
    

    Hope it saves someone's time that I have wasted to resolve this silly issue of MongoDB.

    Also, Read Best Node JS Interview Questions

Please Login or Register to leave a response.

Related Articles

Node Js Tutorials

How to enable cors in Node js

In this article, we are going to see how to enable CORS ( Cross-Origin Resource Sharing ) in Node JS. CORS essentially means cross-domain requests. Cross-Origin Resource Sharing (CORS) is a mechanism ..

Node Js Tutorials

Scheduling CRON Jobs on Node js

Cron Jobs are used for scheduling tasks to run on the server.CRON Jobs are the most commonly used method to automate tasks on Server. In this article, we will see how to schedule Jobs in Node.js. We a..

Node Js Tutorials

Uploading image from url using Node js

In this tutorial, we are going to see how to Upload an image from URL using Node Js. We have written a simple function In Node.js to save an image from URL to local disk/ server. Whenever we use login..