Skip to content

4.11.0: useMongoClient:true incompatible with config.autoIndex #5423

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

Closed
bazineta opened this issue Jun 30, 2017 · 3 comments
Closed

4.11.0: useMongoClient:true incompatible with config.autoIndex #5423

bazineta opened this issue Jun 30, 2017 · 3 comments
Labels
needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue
Milestone

Comments

@bazineta
Copy link

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

If the current behavior is a bug, please provide the steps to reproduce.

The guide at: http://mongoosejs.com/docs/guide.html states, with respect to auto-indexing:

... it is recommended this behavior be disabled in production since index creation can cause a significant performance impact. Disable the behavior by setting the autoIndex option of your schema to false, or globally on the connection by setting the option config.autoIndex to false.

After upgrading to 4.11.0, with config.autoIndex: false set on the connection options, the following warning is emitted:

(node:79221) DeprecationWarning: open() is deprecated in mongoose >= 4.11.0, use openUri() instead, or set the useMongoClient option if using connect() or createConnection()

After adding useMongoClient: true to the connection options, the following is emitted:

the options [config] is not supported

What is the expected behavior?

I'm not sure; I realize this is an in-flux situation. The API documentation and guide seem to be out of sync with one another and it's unclear if the ability to globally disable autoIndexing via a connect() option is still available.

I do hope it still is; it's great peace of mind for production environments, but it'd be error-prone to have to handle this on each schema, rather than globally.

Please mention your node.js, mongoose and MongoDB version.

Node 6.10.2, Mongoose 4.11.0, MongoDB 3.2.11.

@poio1965
Copy link

poio1965 commented Jul 4, 2017

I'd also like to see this fixed/cleared.

@DevBrent
Copy link

DevBrent commented Sep 26, 2018

Documentation states to put autoIndex at the root level. In Mongoose 4.11.14 which apparently has the bugfix for this issue, Mongoose ignores the autoIndex parameter (building indexes against the config's direction) and passes it to MongoDB Client. I moved it to config: { autoIndex: false } while keeping everything else at the root level and this resolved the issue. Unsure if this will also be an issue in 5.X

https://mongoosejs.com/docs/4.x/docs/connections.html#options

var options = {
  useMongoClient: true,
  autoIndex: false, // Don't build indexes
};

@ghost
Copy link

ghost commented Sep 27, 2018

@DevBrent in the latest version of 4.x ( 4.13.17 ), the error the options [autoIndex] is not supported is no longer present and the option is handled correctly. In the latest version of 5.x it also works as advertised. Here are a couple of scripts, one for 4.x and one for 5.x:

5423_5.js on ( [email protected] )

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');
const { Schema, connection} = mongoose;
const GH = 'gh5423';
const URI = `mongodb://localhost:27017/${GH}`;
const OPTS = { useNewUrlParser: true, autoIndex: false };

const schema = new Schema({
  name: {
    type: String,
    unique: true
  }
});

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

const test = new Test({ name: 'test1' });

async function run() {
  await mongoose.connect(URI, OPTS);
  await connection.dropDatabase();
  await test.save();
  let indexes = await Test.collection.listIndexes().toArray();
  console.log(indexes);
  await connection.close();
}

run();

Output:

issues: ./5423_5.js
[ { v: 2, key: { _id: 1 }, name: '_id_', ns: 'gh5423.tests' } ]
issues:

5423_4.js on ( [email protected] )

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const { Schema, connection } = mongoose;
const GH = 'gh5423';
const URI = `mongodb://localhost:27017/${GH}`;
const OPTS = { useMongoClient: true, autoIndex: false };

const schema = new Schema({
  name: {
    type: String,
    unique: true
  }
});

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

const test = new Test({ name: 'test1' });

async function run() {
  await mongoose.connect(URI, OPTS);
  await connection.dropDatabase();
  await test.save();
  let indexes = await Test.collection.listIndexes().toArray();
  console.log(indexes);
  await connection.close();
}

run();

Output:

issues: ./5423_4.js
[ { v: 2, key: { _id: 1 }, name: '_id_', ns: 'gh5423.tests' } ]
issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue
Projects
None yet
Development

No branches or pull requests

4 participants