Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js app crashes when creating indexes for mongodb

When i creating indexes via createIndex or ensureIndex methods my node.js app crashing without providing any details of occured error.

Also i have noticed that all my code works very well with my localhost mongodb until i use remote mongdb atlas replica set.

Node.js: 8.5.0 Mongodb: 3.6.4 Mongodb driver for Node.js: 3.0.8

Example code (but i have tried different variants of implementation, nothing helps):

db.collection('stars').ensureIndex({name: 1}, { unique: true });

I am already listening for uncaughtException and unhandledRejection events, but they dont fire on this crash.

What can i do to get details or get rid of this error?

Thanks for any help!

like image 261
Emil Avatar asked May 28 '18 12:05

Emil


2 Answers

I updated my mongodb driver to version 3.1.0 and eventually got error message with description of reason that causes the problem. According to error message: "The field "retryWrites" is not valid for for an index specification". So, i think the problem was with this field that i used to add to my connection uri string. After removing this field i got rid of this problem.

like image 56
Emil Avatar answered Oct 03 '22 07:10

Emil


The problem might be connected with this issue: NODE-1641. I've got similar problem and found a few solutions:

  • Locking driver version at 3.1.0 and adding useNewUrlParser: true in connection options.
  • In createIndex add field retryWrites: null, so in your case it will look like: db.collection('stars').ensureIndex({name: 1}, { unique: true, retryWrites: null });

However you need to check if it won't turn off retryable writes for that collection.

  • Last option is to vote up that issue and hope that it will be resolved soon, and then update your driver.
like image 21
Paweł Bizoń Avatar answered Oct 03 '22 06:10

Paweł Bizoń