Online Interview Questions

Prepare interviews on Anything, AnyWhere

PHPSCOTS,Blogger, Developer Views: 1089

Dropping an existing index in MongoDB

While working with MongoDB and Node js I face an issue. I have created mu 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_1', 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.