LoginSignup
2
0

More than 3 years have passed since last update.

mongooseでMongoDB に接続しようとしたら身に覚えのない機能に対するWarningが出た

Posted at

はじめに

MongoDB を利用する中、使っていない(特に指定していない)機能に対して Warning が出た。

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

ensureIndex なんて書いた覚えはないので、ナンノコッチャと。Warning なので放置でも良いのだが、気持ち悪いので、解消することに。割と気になる性格。

動作環境

Item version
MongoDB 4.2.8
mongoose 5.10.3
node.js 12.18.1

mongoose の最新は、2020年9月23日現在 5.10.6。週1更新!

解決方法

接続時に createIndex: true を指定すれば良い。

mongoose.connect('mongourl', {
  useCreateIndex: true,
  useNewUrlParser: true,
  useUnifiedTopology: true
})

もしくは、

mongoose.set('useCreateIndex', true)
mongoose.connect('mongourl', {
  useNewUrlParser: true,
  useUnifiedTopology: true
})

など。
なお、エラーメッセージでは、createIndex"es" を使えと書いてあるが、指定してもそんなものはないと怒られる。

おしまい。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0