using ensureIndex in mongodb schema using mongoose

后端 未结 4 1646
走了就别回头了
走了就别回头了 2021-02-06 01:40

I would like to call ensureIndex on the authorName, what is the command and where in this code should I put it?

var mongoose = require(         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 02:10

    You could use this statement:

    mongoose.connection.collections['my_collection'].ensureIndex({ "key": 1 }, { "unique": true }, callback);
    

    For example you want to do some integration tests, so you will need to drop your collections rapidly. In that case mongoose doesn't setup indexes again during runtime even if option autoIndex is set to true. This answer could be useful in that case.

提交回复
热议问题