mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Docs page for how to fix deprecation warnings

Open vkarpov15 opened this issue 8 years ago • 31 comments

After #6917 it should now be possible to clear up all deprecation warnings by:

  • Replacing update() with updateOne(), updateMany(), or replaceOne()
  • Replacing remove() with deleteOne() or deleteMany()
  • Setting mongoose.set('useFindAndModify', false);
  • Setting mongoose.set('useCreateIndex', true);
  • Setting mongoose.set('useNewUrlParser', true);

Need a docs page to summarize this.

vkarpov15 avatar Aug 27 '18 01:08 vkarpov15

If these are set, would it be incompatible with or breaking the current program?

islishude avatar Aug 27 '18 07:08 islishude

@isLishude the only differences we are aware of are:

  1. Support for MongoDB 3.0.x
  2. updateOne() and updateMany() do not support the overwrite option. If you use update(filter, update, { overwrite: true }), you should use replaceOne(filter, update), not updateOne(filter, update, { overwrite: true })

If you run into any other problems, please don't hesitate to open up a GitHub issue.

vkarpov15 avatar Aug 27 '18 12:08 vkarpov15

I set mongoose.set('useCreateIndexes', true) and still get DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

I've tried it before and after mongoose.connect().

ulrichb avatar Aug 27 '18 14:08 ulrichb

@ulrichb Are you sure that you're using [email protected] that released an hour ago?

Fonger avatar Aug 27 '18 14:08 Fonger

I can replicate what @ulrichb is seeing on 5.2.10 with:

6922.js

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

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
mongoose.set('useCreateIndexes', true);

const conn = mongoose.connection;
const Schema = mongoose.Schema;

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

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

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

async function run() {
  console.log(`mongoose version: ${mongoose.version}`);
  await conn.dropDatabase();
  await test.save();
  return conn.close();
}

run();

Output:

issues: ./6922.js
mongoose version: 5.2.10
(node:7186) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
issues:

ghost avatar Aug 27 '18 14:08 ghost

I believe it is mongoose.set('useCreateIndex', true)

saagar2000 avatar Aug 27 '18 15:08 saagar2000

updated the original comment to reflect @saagar2000's observation. Thanks @saagar2000 !

ghost avatar Aug 27 '18 15:08 ghost

@vkarpov15 is it possible to rename the document method remove to delete to be consistent as well? (Or provide an alias)?

Currently I'm refactoring all Model based usage of remove to deleteOne and deleteMany, but the document methods are still called remove() (which is also inconsistent with the isDeleted() method).

E.g.

const Foo = mongoose.model('Foo');
const foo = new Foo();
foo.remove(); //<-- create alias/rename to foo.delete()

adamreisnz avatar Aug 29 '18 02:08 adamreisnz

I'm seeing the collection.{remove|update} is deprecated warning on Document.{remove|update} operations.

const doc = await model.findById(someId);
await doc.update({ $set: { someProp: true }}); // Deprecation warning
await doc.remove() // Deprecation warning

ghost avatar Aug 29 '18 14:08 ghost

@adamreisnz yeah we should add an alias doc.delete(), ditto for doc.update() @mycompassspins

vkarpov15 avatar Aug 29 '18 19:08 vkarpov15

mongoose.set('useCreateIndex', true) has no affect for me. 😞 still getting the 'DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.'

ParikshitChavan avatar Sep 03 '18 03:09 ParikshitChavan

@ParikshitChavan Did you set this option before calling mongoose.model()?

Fonger avatar Sep 03 '18 11:09 Fonger

Using mongoose 5.2.12. I also get this: the options [useCreateIndex] is not supported in addition to the deprecation warning.

More info: I only get this extra message when pass useCreateIndex as a connection option. When i use mongoose.set() I don't get that message.

govindrai avatar Sep 03 '18 17:09 govindrai

looks like it should work properly when pass as connection option, but it doesn't. only after explicit mongoose.set()

pumano avatar Sep 03 '18 21:09 pumano

@Fonger @govindrai I tried doing this through connection options and though mongoose.set() before and after connection to the DB. the 'DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.' always seems to persist.

ParikshitChavan avatar Sep 04 '18 05:09 ParikshitChavan

Same here, cant get rid of collection.ensureIndex is deprecatedwarning ... No matter if use set or as an option...

Avcajaraville avatar Sep 04 '18 09:09 Avcajaraville

@ParikshitChavan @govindrai @Avcajaraville

You have to put mongoose.set('useCreateIndex', true) before EVERY mongoose.model() call (Note: not mongoose.connect() ).

See the discussion in #6890. This is fixed in 5.2.13 (but it's not released yet).

Fonger avatar Sep 04 '18 11:09 Fonger

@Fonger @vkarpov15 after upgrade to 5.2.13 the issue still exists

(node:24769) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

package.json:

"dependencies": {
		"bcrypt": "^3.0.0",
		"connect-mongo": "^2.0.1",
		"ejs": "^2.6.1",
		"express": "^4.16.3",
		"express-session": "^1.15.6",
		"mongodb": "^3.1.4",
		"mongoose": "^5.2.13",
		"morgan": "^1.9.0",
		"passport-local": "^1.0.0"
	},

node and npm info:

$ node -v
v10.6.0

$ npm -v
6.4.1

arastu avatar Sep 05 '18 23:09 arastu

@arastu can you create a reproducible example with one or more files where you demonstrate that you are using 5.2.13 and are seeing one or more of these warnings?

Note that I am calling mongoose.set() before requiring my models

For Example:

index.js

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

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
const conn = mongoose.connection;


const One = require('./one');
const Two = require('./two');

const one = new One({ name: '1' });
const two = new Two({ name: '2' });

async function run() {
  assert.strictEqual(mongoose.version, '5.2.13');
  await conn.dropDatabase();
  await Promise.all([One.init(), Two.init()]);
  await one.save();
  await two.save();
  await One.findOneAndUpdate({}, { name: 'one' });
  await Two.findOneAndUpdate({}, { name: 'two' });
  console.log(`${mongoose.version} should show no deprecations with the new settings.`);
  return conn.close();
}

run();

one.js

'use strict';

const mongoose = require('mongoose');

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

module.exports = mongoose.model('one', schema);

two.js

'use strict';

const mongoose = require('mongoose');

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

module.exports = mongoose.model('two', schema);

Output:

6922: ./index.js
5.2.13 should show no deprecations with the new settings.
6922:

ghost avatar Sep 06 '18 00:09 ghost

working for me in 5.2.13. @lineus will useCreateIndex be supported as a connection option? Currently getting the options [useCreateIndex] is not supported

i.e. mongoose.connect( process.env.MONGODB_URI, { useNewUrlParser: true, autoIndex: false, useCreateIndex: true, } )

govindrai avatar Sep 07 '18 23:09 govindrai