Using makeIndex in a mongodb scheme using mongoose

I would like to name ensureIndex in authorName , what kind of command is this and where should I put it in this code?

 var mongoose = require('mongoose'); // defines the database schema for this object var schema = mongoose.Schema({ projectName : String, authorName : String, comment : [{ id : String, authorName : String, authorEmailAddress : { type : String, index : true } }] }); // Sets the schema for model var ProjectModel = mongoose.model('Project', schema); // Create a project exports.create = function (projectJSON) { var project = new ProjectModel({ projectName : projectJSON.projectName, authorName : projectJSON.authorName, comment : [{ id : projectJSON.comments.id, authorName : projectJSON.comments.authorName, authorEmailAddress : projectJSON.authorEmailAddress }); project.save(function(err) { if (err) { console.log(err); } else{ console.log("success"); } }); }); } 
+7
source share
4 answers

You do not invoke ensureIndex directly, you indicate that the field should be indexed in your schema as follows:

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

Based on this definition, Mongoose will call ensureIndex for you when you register the model with a call to mongoose.model .

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

 mongoose.set('debug', true); 
+21
source

You can use this statement:

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

For example, you want to perform some integration tests, so you need to quickly drop your collections. In this case, mongoose does not set indexes again at run time, even if autoIndex set to true . This answer may be useful in this case.

+11
source

you can call the Schema # index method to create the index

 let urlSchema = new Schema({ url: String, status: Number } ); urlSchema.index({ url: 1 }, { unique: true, background: true, dropDups: true }); 

you can listen to the creation of the index event.

 let UrlModel = mongoose.model('url', urlSchema); UrlModel.on('index', function(error) { if (error && error.message) { console.log(`Url collection create index error:${error.message}`); } }); 

Note: the index creation process is asynchronous. When you create a unique index, you cannot insert duplicate data. or index creation fails;

+2
source

First define the index in the authorName field, and if you manually want to call makeIndex due to a specific requirement, you need to set autoIndex to false . Here is what your circuit looks like:

 var schema = mongoose.Schema({ projectName : String, authorName : {type : String, index : true} comment : [{ id : String, authorName : String, authorEmailAddress : { type : String, index : true } }] }, { // Turn-off auto indexing, we manually need to trigger indexing autoIndex : false }); 

And based on this requirement, you can call the secureIndexes method for the model that you created using this scheme, i.e. ProjectModel.ensureIndexes ();

+1
source

All Articles