using ensureIndex in mongodb schema using mongoose

后端 未结 4 1647
走了就别回头了
走了就别回头了 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:20

    You don't call ensureIndex directly, you indicate that field should be indexed in your schema like this:

    var schema = mongoose.Schema({
      projectName : String,
      authorName : { type: String, index: true }
    });
    

    Based on that definition, Mongoose will call ensureIndex for you when you register the model via the mongoose.model call.

    To see the ensureIndex calls that Mongoose is making, enable debug output by adding the following to your code:

    mongoose.set('debug', true);
    

提交回复
热议问题