-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead. #6890
Comments
duplicate of #6880 |
Thanks for taking the time to report the issue @bricss. Mongoose calls |
,unique: true,index: true is on one field in one of my schemas and it triggers this warning. Question how to ensure a unique value without getting this warning. |
@nonniv upgrade to 5.2.10 and set |
After upgrading to version 5.2.10. Any of the options below should stop the warnings? mongoose.connect(config.dbUri, {
useCreateIndex: true,
useNewUrlParser: true
}) or mongoose.set('useCreateIndex', true)
mongoose.connect(config.dbUri, { useNewUrlParser: true }) It didnt work here. (Sorry if I did something wrong) |
@samuelcecilio what specific deprecation warnings are you seeing? There are some helpful notes in the comment that opened #6922, or feel free to share your specific warnings here and I'll see if I can help. |
|
@samuelcecilio the 6922.js#!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
mongoose.set('useCreateIndex', 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:
are you certain that your project is using 5.2.10? are there other packages in your project using other versions of mongoose, etc? |
Hi, thanks for the reply. Yes, I checked the version three times lol:
My code app.js ...
const config = require('./config')
const mongoose = require('mongoose')
const express = require('express')
const app = express()
mongoose.connect(config.dbUri, { useNewUrlParser: true })
mongoose.set('useCreateIndex', true)
....
module.exports = app bin/www #!/usr/bin/env node
const app = require('../app')
const config = require('../config')
const debug = require('debug')('blitz')
const http = require('http')
const port = normalizePort(process.env.PORT || config.port)
const mongoose = require('mongoose')
app.set('port', port)
const server = http.createServer(app)
server.listen(port)
server.on('error', onError)
server.on('listening', onListening)
const mongo = mongoose.connection
mongo.on('error', error => { debug('mongo: ' + error.name) })
mongo.on('connected', () => { debug('mongo: Connected') })
mongo.on('disconnected', () => { debug('mongo: Disconnected') })
.... When I run > nodemon bin/www
[nodemon] 1.18.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node bin/www`
blitz Listening on port 3000 +0ms
blitz mongo: Connected +14ms
(node:10622) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead. |
@samuelcecilio maybe move the |
@samuelcecilio after playing around with this some, I found that I needed to set useCreateIndex to true in the route file that contained the model. In my case, after using the express cli tool, I needed to set useCreateIndex in the index route shown below: var express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
mongoose.set('useCreateIndex', true);
const Test = mongoose.model('test', { name: {type: String, unique: true }});
/* GET home page. */
router.get('/', function(req, res, next) {
Test.create({ name: 'billy' }).then((doc) => {
return res.status(200).json({ done: true, test: doc.id });
}).catch((e) => {
return res.status(500).json({ err: e });
});
});
module.exports = router; without the option set in the index route I got:
after setting the option in the route I got:
I tried setting it in bin/www but that did not get rid of the warning. Only setting it in the route was successful. output of curling localhost
|
I use separate files for users, customers, suppliers, orders, items.. This stopped the warning: models/user.js const addressSchema = require('./address')
const profileSchema = require('./profile')
const uuidv4 = require('uuid/v4')
const mongoose = require('mongoose')
const Schema = mongoose.Schema
mongoose.set('useCreateIndex', true)
const userSchema = new Schema({
disabled: { type: Boolean, default: false },
roles: { type: Array, default: [ 'assistant' ] },
identity: { type: String, unique: true, required: true },
username: { type: String, unique: true, required: true },
name: { type: String, uppercase: true, required: true },
password: { type: String, required: true },
secret: String,
profile: profileSchema,
address: addressSchema,
created: Date,
modified: Date
}, {
collection: 'users'
})
userSchema.pre('save', function (next) {
const doc = this
doc.created = Date.now()
doc.modified = Date.now()
doc.secret = uuidv4()
next()
})
module.exports = mongoose.model('User', userSchema) I figured it would be some global setting, but apparently I have to add in all the files. |
@samuelcecilio you could wrap mongoose in a custom file, something like myMongoose.jsconst mongoose = require('mongoose')
mongoose.set('useCreateIndex', true);
//etc
module.exports = mongoose and then require your wrapper |
Still I would have to modify all current files. For each file in the The question is: in a future version of |
I suspect the behavior will remain as it is now ( having to set the variable, not the internal implementation of it ) until the next major version (ie 6.0.0 ) of mongoose. |
For now, I've added Should this be global? Is it an express issue? |
It's really uncomfortable to add |
@samuelcecilio @bricss Agreed. Reopening this to investigate potential solutions. |
You don't need to put this in every file, but unfortunately it looks like right now you need to put mongoose.set('useCreateIndex', true) before any mongoose.model() calls. That is tricky if you export models as opposed to schemas. Will add a fix for this. |
After upgrading to version 5.2.13, should this stop the warnings? Thanks mongoose.connect(config.dbUri, {
useCreateIndex: true,
useNewUrlParser: true
}) the options [useCreateIndex] is not supported
server: listening on port 3000 +0ms
mongo: Connected +34ms
(node:18457) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead. |
@samuelcecilio |
It works with 5.2.13 after connection and before all other files. This is connection in my db.js file:
|
I have my mongo connection in a separate script. This way I just call mongoose.set('useCreateIndex', true); right before actually connecting to mongo. Then there's no need to put it in any of my model declarations. |
@LucHighwalker 's suggestion is the right way to do this. Ideally you put all these global options right before your call to |
Is there any functional difference between
and
? Because both seem to work just fine in 5.2.17 |
Works with the latest version |
Okay, i guess @types/mongoose is some versions behind then (doesn't work on typescript yet). |
|
@Dionesio's answer did the trick to me. Thanks! |
mongoose.connect("mongodb://localhost:27017/test", {useNewUrlParser: true}); This solved all my deprecation warnings. |
Thanks - Adding mongoose.set('useCreateIndex', true); fixed my issue |
I'm going to lock this issue. For future reference, see the deprecations guide on our docs |
After update to version
5.2.9
I'm getting this message in console all the time, when I start my web app:DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
And I don't use neither of them.
The text was updated successfully, but these errors were encountered: