Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with indexes (the indexes are not created) #5317

Closed
gustavomanolo opened this issue Jun 1, 2017 · 3 comments
Closed

Bug with indexes (the indexes are not created) #5317

gustavomanolo opened this issue Jun 1, 2017 · 3 comments
Milestone

Comments

@gustavomanolo
Copy link

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

@whyhankee
Copy link
Contributor

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.

@alianrock
Copy link

alianrock commented Jun 10, 2017

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

@vkarpov15
Copy link
Collaborator

Yeah this is a duplicate of #5324 and #5328 . Will be fixed with 4.10.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants