DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.
"mongoose": "5.3.1",
@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:
#!/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();
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:
#!/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();
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.
DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.
I got the warning msg if findByIdAndUpdate
used 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
Most helpful comment
I got the warning msg if
findByIdAndUpdate
used but nofindAndModify
in the project.