HI i have a model used to store likes for a "news" model like this:
import mongoose, { Schema } from 'mongoose';
mongoose.set('debug', true);
var newsLikesSchema = new Schema({
_news_id: { type: Schema.Types.ObjectId, ref: 'news' },
condominiums_id: { type: Number, required: true },
users_id: { type: Number, required: true },
created_at: { type: Date, default: Date.now },
}, {
emitIndexErrors: true
});
newsLikesSchema.on('error', function(errorE) {
console.log('---> index error: ', errorE);
});
newsLikesSchema.on('index', function(errI) {
console.log('----> new index creating', errI);
});
// Index
newsLikesSchema.index({ _news_id: 1, users_id: 1 }, {unique: true}); // unique like per news/nuser
newsLikesSchema.index({ _news_id: 1, created_at: 1 }); // to sort by news_id and date
newsLikesSchema.index({ users_id: 1 });
var newsLikesM = mongoose.model('newslikes', newsLikesSchema);
module.exports = newsLikesM;
The problem is that the indexes are not created and i don't have feedback from the ".on('error')" and ".on('index')" callbacks, they're not being executed.
MongoDB version: 3.4.2
Mongoose version: 4.10.4
NodeJS version: v6.7.0
I had a problem with index creation as well. In my case it was related to schema.options.autoIndex.
schema.options.autoIndex = false for my tables and I call ensureIndexes in a seperate process.
However, indexes were not created since it still checked for the autoIndex flag.
You can check if #5324 fixes your problem.
I set autoIndex:false, and call the Model.ensureIndexes() to build the index, but index not create. Is that related to the issue?
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var uniqueValidator = require('mongoose-unique-validator');
mongoose.set('debug', true);
mongoose.connect('mongodb://localhost:27017/gh3396',{ config: {autoIndex: false} });
// Variables
var ClientSchema = new Schema(
{
name : {
type : String,
required : true,
trim : true
},
organization : {
type : String,
required : true
}
}
)
// Indexes
ClientSchema.index({ "name" : 1, "organization" : 1 }, { unique : true })
// Plugins
ClientSchema.plugin(uniqueValidator, {
message : 'Name must be unique.'
})
const Client = mongoose.model('Client', ClientSchema);
Client.ensureIndexes()
Client.on('index', function(err){
console.log(err)
})
new Client({
name: 'a',
organization:'a'
}).save().then((result) => {
console.log(result)
}, (err) => {
console.log(err)
})
and it just create _id index as default:
> use gh3396
switched to db gh3396
> show collections
clients
> db.clients.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "gh3396.clients"
}
]
Any ideas?
MongoDB version: 3.2.1
Mongoose version: 4.10.3
NodeJS version: v6.7.0
Yeah this is a duplicate of #5324 and #5328 . Will be fixed with 4.10.6.
Most helpful comment
I set autoIndex:false, and call the Model.ensureIndexes() to build the index, but index not create. Is that related to the issue?
and it just create _id index as default:
Any ideas?
MongoDB version: 3.2.1
Mongoose version: 4.10.3
NodeJS version: v6.7.0