Mongoose does not create an index

I recently started using Mongoose (v.3.2.1) and I am having problems with indexes.

I define a pair of indexes of my schema (Schema.path ('attr'). Index (true)) and they are not created in the database. (I run db.collection.getIndexKeys () in the shell, and I only see the _id index).

Note that some / all indexes are sometimes created.

I turned on debugging and I see that makeIndex () works:

mongoose.set('debug', true); Mongoose: myColl.ensureIndex({ type: 1 }) { safe: true, background: true } Mongoose: myColl.ensureIndex({ created: 1 }) { safe: true, background: true } Mongoose: myColl.ensureIndex({ updated: 1 }) { safe: true, background: true } 

I also listened to errors:

 mongoose.connection.on('error', function(err) { console.error('MongoDB error: %s', err); }); myCollModel.on('index',function(err) { console.error('MongoDB error: %s', err); }); 

I see my inserts and requests in the console, but without errors.

Any help would be greatly appreciated!

Thanks,

Nitzan bar

My schema is defined as follows:

 myColl = new Schema(); myColl.add({ text : { type: String, required : true } , shortText: { type: String } , type : { type: String , index: true} , correctAnswerId : { type: ObjectId, ref: QuestionAnswer} , answers: { type: [ QuestionAnswer ] } }); 

In this case, the type index is not created. Note that sometimes after running makeIndexes () they are created a couple of times.

Thanks!

+4
source share
2 answers

When defining a field that refers to the ObjectId of a document in another collection, the value of the ref attribute is the name of the model string, not the model itself. Therefore, it should look like this:

 correctAnswerId : { type: ObjectId, ref: 'QuestionAnswer' } 

I have a feeling that the time of this failure affects whether the type index is created or not.

0
source

In fact, I had the same problem.

And then I copy the makeindex debugging information to the mongo terminal to try, the command was sent (as shown in the mongoose console), but the creation failed due to the mongo problem:

 { "err" : "ns name too long, max size is 128", "code" : 10080, "n" : 0, "connectionId" : 78, "ok" : 1 } 

then I give the option {name: 'super_index'}, then this is done.

Hope this is helpful for you!

0
source

Source: https://habr.com/ru/post/1438470/


All Articles