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

Docs page for how to fix deprecation warnings #6922

Closed
vkarpov15 opened this issue Aug 27, 2018 · 31 comments
Closed

Docs page for how to fix deprecation warnings #6922

vkarpov15 opened this issue Aug 27, 2018 · 31 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@vkarpov15
Copy link
Collaborator

vkarpov15 commented Aug 27, 2018

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 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Aug 27, 2018
@vkarpov15 vkarpov15 added this to the 5.2.11 milestone Aug 27, 2018
@islishude
Copy link

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

@vkarpov15
Copy link
Collaborator Author

@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.

@ulrichb
Copy link

ulrichb commented Aug 27, 2018

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().

@Fonger
Copy link
Contributor

Fonger commented Aug 27, 2018

@ulrichb Are you sure that you're using mongoose@5.2.10 that released an hour ago?

@lineus
Copy link
Collaborator

lineus commented Aug 27, 2018

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:

@saagar2000
Copy link

saagar2000 commented Aug 27, 2018

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

@lineus
Copy link
Collaborator

lineus commented Aug 27, 2018

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

@adamreisnz
Copy link
Contributor

adamreisnz commented Aug 29, 2018

@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()

@ghost
Copy link

ghost commented Aug 29, 2018

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

@vkarpov15
Copy link
Collaborator Author

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

@vkarpov15 vkarpov15 modified the milestones: 5.2.11, 5.2.12 Aug 30, 2018
@ParikshitChavan
Copy link

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

@Fonger
Copy link
Contributor

Fonger commented Sep 3, 2018

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

@govindrai
Copy link

govindrai commented Sep 3, 2018

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.

@pumano
Copy link

pumano commented Sep 3, 2018

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

@ParikshitChavan
Copy link

@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.

@Avcajaraville
Copy link

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

@Fonger
Copy link
Contributor

Fonger commented Sep 4, 2018

@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).

@arastu
Copy link

arastu commented Sep 5, 2018

@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

@lineus
Copy link
Collaborator

lineus commented Sep 6, 2018

@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:

@vkarpov15 vkarpov15 added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature and removed docs This issue is due to a mistake or omission in the mongoosejs.com documentation labels Sep 10, 2018
@vkarpov15 vkarpov15 modified the milestones: 5.2.15, 5.2.16 Sep 11, 2018
@ghost
Copy link

ghost commented Sep 11, 2018

I've upgraded to 5.2.14 and am still seeing deprecation warnings that don't make sense. For instance, collection.update is deprecated . . . when I'm actually using document.update. Same for the remove method of both the model and the document. Switching to document.updateOne produces TypeError: document.updateOne is not a function. Am I missing something here?

@Avcajaraville
Copy link

@mycompassspins Did you track the stack producing the warning ?

For instance, in my case turns out the deprecation warning was happening due to connect mongo.

If you follow the above instructions, at least for me, all deprecation warning where gone :)

@ghost
Copy link

ghost commented Sep 12, 2018

Here's the stack:

DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
    at NativeCollection.(anonymous function) [as update] (/MyProject/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:143:28)
    at NodeCollection.update (/MyProject/node_modules/mquery/lib/collection/node.js:66:19)
    at model.Query._updateThunk (/MyProject/node_modules/mongoose/lib/query.js:3233:23)
    at model.Query.Query._execUpdate (/MyProject/node_modules/mongoose/lib/query.js:3245:23)
    at process.nextTick (/MyProject/node_modules/kareem/index.js:333:33)
    at process._tickCallback (internal/process/next_tick.js:150:11)

Obviously, Model.update is actually executed, even though I'm calling MongooseDocument.update. This is what I'm doing, more or less:

async UpdateMyDocument(id:string, update:MyDocument):Promise<MyDocument>
{
	const doc = await MyModel.findById(id)
		.populate({ path: 'somePath', populate: [{ path: 'somePath.nested' }] });

	return await doc.update(update); // <= MongooseDocument.update is producing a Model.update deprecation warning
}

@vkarpov15
Copy link
Collaborator Author

@mycompassspins are you sure you're on 5.2.14? We added doc.updateOne(update) in 5.2.13

joe4dev added a commit to joe4dev/express-rest-api that referenced this issue Sep 18, 2018
Alternative patch implementation using Mongoose `findByIdAndUpdate`:
https://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate

Notice:
The `new` determines whether to return the new (i.e., updated) version (default: false)

Caveats:
Mongoose is in the process of changing their API: Automattic/mongoose#6922 (comment)
To fix the deprecation warnings, we need to set:

```js
mongoose.set('useFindAndModify', false);
```
joe4dev added a commit to joe4dev/express-rest-api that referenced this issue Sep 18, 2018
Alternative patch implementation using Mongoose `findByIdAndUpdate`:
https://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate

Notice:
The `new` determines whether to return the new (i.e., updated) version (default: false)

Caveats:
Mongoose is in the process of changing their API: Automattic/mongoose#6922 (comment)
To fix the deprecation warnings, we need to set:

```js
mongoose.set('useFindAndModify', false);
```
@lineus
Copy link
Collaborator

lineus commented Sep 21, 2018

There's an internal configuration typo in @5.2.16 that causes the deprecation notice for createIndexes to reemerge when you call createIndexes explicitly. PR opened☝️

@lineus lineus reopened this Sep 21, 2018
@nerdophile
Copy link

nerdophile commented Oct 7, 2018

(node:6455) DeprecationWarning: GridStore is deprecated, and will be removed in a future version. Please use GridFSBucket instead. What about this ??.
"gridfs-stream": "^1.1.1",
"mongoose": "^5.3.1",
"multer": "^1.4.0",
"multer-gridfs-storage": "^3.0.0"

@vkarpov15
Copy link
Collaborator Author

@venkyyPoojari that's an old one, predates mongoose 5, but worth addressing. We will fix.

@vkarpov15 vkarpov15 reopened this Oct 11, 2018
@vkarpov15 vkarpov15 modified the milestones: 5.2.16, 5.3.5 Oct 11, 2018
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature labels Oct 11, 2018
@nerdophile
Copy link

Thanks, I am going to use this for production, so please help as soon as possible

@romain10009
Copy link

Thanks, I am going to use this for production, so please help as soon as possible

I have the same issue, I'm piping the readstream to the res object, is this what is wrong? I mean does it prevent the files to be read out?

@vkarpov15 vkarpov15 modified the milestones: 5.3.5, 5.3.4 Oct 15, 2018
@vkarpov15
Copy link
Collaborator Author

@venkyyPoojari @romain10009 that's because gridfs-stream is out of date. The MongoDB driver has a new streaming API for GridFS that you should use instead: https://thecodebarbarian.com/mongodb-gridfs-stream

@Automattic Automattic locked as too heated and limited conversation to collaborators May 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests