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

Indicate index won't be created when not buffering #11711

Closed
wants to merge 1 commit into from

Conversation

Ehbraheem
Copy link

Updates documentation to show the caveat of turning off buffering.

Summary

Mongoose doesn't create indexes automatically when buffering is turned off. Possible reason for #10631

Examples

const mongoose = require('mongoose');
const {Schema} = mongoose;

const testSchema = new Schema({
    name: String,
    age: Number,
    weight: Number,
    height: Number,
    birthday: String
});

testSchema.index({age: 1}, {unique: true});
testSchema.index({ name: 1, weight: 1});
testSchema.index({ name: 1, birthday: 1}, {unique: true});
testSchema.index({ name: 1, height: 1});

testSchema.index({'$**': 'text'}, { name: 'textIndex'});

const Test = mongoose.model('Test', testSchema, 'test');

async function test() {
    await mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        bufferCommands: false
      });
      await mongoose.connection.dropDatabase();
      await Test.init();
    console.log(await Test.listIndexes());
}

test();

Example is taken from @IslandRhythms' #10631 (comment)

Updates documentation to show the caveat of turning off buffering.
@vkarpov15 vkarpov15 added this to the 6.3.3 milestone May 2, 2022
@vkarpov15
Copy link
Collaborator

I took a quick look and your script shows that Mongoose does in fact create indexes if bufferCommands is disabled in v6.3.2. Below is the script output:

[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  {
    v: 2,
    key: { age: 1 },
    name: 'age_1',
    background: true,
    unique: true
  },
  {
    v: 2,
    key: { name: 1, weight: 1 },
    name: 'name_1_weight_1',
    background: true
  },
  {
    v: 2,
    key: { name: 1, birthday: 1 },
    name: 'name_1_birthday_1',
    background: true,
    unique: true
  },
  {
    v: 2,
    key: { name: 1, height: 1 },
    name: 'name_1_height_1',
    background: true
  },
  {
    v: 2,
    key: { _fts: 'text', _ftsx: 1 },
    name: 'textIndex',
    background: true,
    weights: { '$**': 1 },
    default_language: 'english',
    language_override: 'language',
    textIndexVersion: 3
  }
]

What version of Mongoose are you using? Based on useUnifiedTopology, it looks like you're using some 5.x version?

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label May 6, 2022
@vkarpov15 vkarpov15 removed this from the 6.3.3 milestone May 6, 2022
@javierguzman
Copy link

Hello @vkarpov15 I have just updated my project packages, including Moongose from 5.12.13 to 6.3.2. My tests have started to fail due to indexes not being created. In order to figure out what it is going on I have added this:

DestinationModel.on('index', err => {
  console.log('Destination model indexes done');
  if (err) {
    console.log(err);
  }
});

Originally, I set bufferCommands to false, but I can only see the message "Destination model indexes done" if bufferCommands is set to true

@Uzlopak
Copy link
Collaborator

Uzlopak commented May 16, 2022

If the collection already exists i think the indizes are not generated.

@vkarpov15
Copy link
Collaborator

@javierguzman can you please open a new issue and follow the issue template? bufferCommands should generate indexes.

@Uzlopak Mongoose will call createIndexes() regardless of whether the collection exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants