node.js - Mongoose is not recreating the index collection is dropped -


i know should removeall documents instead of dropping collection. why mongoose not recreate index after collection dropped?

i have schema in 1 of fields has unique:true set. index created when start server. if drop collection, , new records come, collection recreated , records inserted. but, index unique field not created again.

why so? how ask recreate index?

it has nothing unique. if drop index in mongodb, have restart node.

when application starts up, mongoose automatically calls ensureindex each defined index in schema. mongoose call ensureindex each index sequentially, , emit 'index' event on model when ensureindex calls succeeded or when there error. while nice development, recommended behavior disabled in production since index creation can cause significant performance impact.

nevertheless, in development mode, can recreate indexes in method (for example method tied route or event) with:

(mongodb > 3.0) db.collection.createindex({})  (mongodb < 3.0) db.collection.ensureindex({}) 

with mongoose looks this:

model.ensureindexes(function (err) {   if (err) {       return res.end(err);   } }); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -