Mongoose: DeprecationWarning: collection.findAndModify is deprecated

Created on 8 Oct 2018  路  8Comments  路  Source: Automattic/mongoose

https://github.com/Automattic/mongoose/blob/5c0e444ae8c50e352de5c4f7a763419fbb4dd316/lib/query.js#L2727

DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.

"mongoose": "5.3.1",

Most helpful comment

snip20181024_159

DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.

I got the warning msg if findByIdAndUpdateused but no findAndModify in the project.

node: '10.0.0'
npm: '5.6.0'
mongod: v4.0.2
mongoose: '^5.3.6'

All 8 comments

@Dev-Wito In [email protected] you can use mongoose.set('useFindAndModify', false). There's a guide here with more details. Here's an example script and it's output:

7108.js

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

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('useFindAndModify', false);
const { Schema, connection} = mongoose;
const DB = '7108';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = { family: 4, useNewUrlParser: true };

const schema = new Schema({
  name: String
});

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

async function run() {
  assert.strictEqual(mongoose.version, '5.3.1');
  await mongoose.connect(URI, OPTS);
  await connection.dropDatabase();
  const cond = { name: 'test' };
  const update = cond;
  const opts = { new: true, upsert: true };
  await Test.findOneAndUpdate(cond, update, opts);
  const created = await Test.findOne({});
  assert.strictEqual(created.name, 'test');
  console.log('All Assertions Pass.');
  await connection.close();
}

run();

Output:

issues: ./7108.js
All Assertions Pass.
issues:

there are also several closed issues for this with more details here's a link to a search that should find most, if not all of them.

It seems like there may be an issue with setting useFindAndModify in the connection options in [email protected] though

Repro script:

7059.js

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

const assert = require('assert');
const mongoose = require('mongoose');
const { Schema, connection} = mongoose;
const DB = '7059';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = { family: 4, useNewUrlParser: true, useFindAndModify: false };

const schema = new Schema({
  name: String
});

async function run() {
  assert.strictEqual(mongoose.version, '5.3.2');
  await mongoose.connect(URI, OPTS);
  await connection.dropDatabase();
  const Test = mongoose.model('test', schema);
  const cond = { name: 'test' };
  const update = cond;
  const opts = { new: true, upsert: true };
  await Test.findOneAndUpdate(cond, update, opts);
  const created = await Test.findOne({});
  assert.strictEqual(created.name, 'test');
  console.log('All Assertions Pass.');
  await connection.close();
}

run();

Output:

issues: ./7059.js
(node:54429) DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.
All Assertions Pass.
issues:

I'll dig a bit deeper into this.

useFindAndModify in connection options is fixed in 5.3.4.

snip20181024_159

DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.

I got the warning msg if findByIdAndUpdateused but no findAndModify in the project.

node: '10.0.0'
npm: '5.6.0'
mongod: v4.0.2
mongoose: '^5.3.6'

My mistake, apologies for annoyance.
fixed by mongoose.set('useFindAndModify', false);

is this an acceptable answer though?

I find it odd that users will need to suppress a deprecation warning, for a usage of a depreciated method within the mongoose package ( or its dependencies)

@janakagoon I agree it is a bit strange, but this is the only way we can do this without making a backwards breaking change.

I'm going to lock this issue. If you stumble across this issue, please check out the docs: https://mongoosejs.com/docs/deprecations.html

Was this page helpful?
0 / 5 - 0 ratings