MongoDB suddenly stop responding because of background processes

Our mongo DB suddenly stops responding to any request and throws error messages like:

     MongoError: cannot perform operation: a background operation is currently running for collection demo2.users
         at MessageStream.messageHandler (....../node_modules/mongodb/lib/cmap/connection.js:268:20)
         at MessageStream.emit (events.js:315:20)
         at processIncomingData (...../node_modules/mongodb/lib/cmap/message_stream.js:144:12)
         at MessageStream._write (....../node_modules/mongodb/lib/cmap/message_stream.js:42:5)
         at writeOrBuffer (internal/streams/writable.js:358:12)
         at MessageStream.Writable.write (internal/streams/writable.js:303:10)
         at Socket.ondata (internal/streams/readable.js:719:22)
         at Socket.emit (events.js:315:20)
         at addChunk (internal/streams/readable.js:309:12)
         at readableAddChunk (internal/streams/readable.js:284:9)
         at Socket.Readable.push (internal/streams/readable.js:223:10)
         at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
       ok: 0,
       code: 12587,
       codeName: 'BackgroundOperationInProgressForNamespace'
    }

We are using MongoDB driver with node.js. MongoDB and node.js are on the same server (Ubuntu Linux 20.04.2)

For reusing the mongo connections we use the npm generic-pool library, and only when we start the server we setup at the beginning all the indexes and the collections with the commands:

  const existingCollection = await mongo.db(databaseName).listCollections({ name }).next();
  if (existingCollection) {
    await mongo.db(databaseName).command({
      collMod: name,
      validator: { $jsonSchema: schema },
      validationLevel: 'strict',
      validationAction: 'error',
    });
  } else {
    await mongo.db(databaseName).createCollection(name, {
      validator: { $jsonSchema: schema },
      validationLevel: 'strict',
      validationAction: 'error',
    });
  }

and ensuring indexes with

  await mongo.db(databaseName).collection(collectionName).createIndex({
    clientNumber: 1,
  }, { unique: true });

But it looks like mongo starts suddenly to make indexing, validations, and other background operations that lock everything - this happens mostly after few days of a running the server.

The only way mongo starts again to respond is to restart it.

Has anyone any idea what could be the cause of this? Or is there any way to stop any locking background operations of mongo?

Welcome to the MongoDB Community @Anton_Tonchev!

To help understand this problem, can you please provide:

  • the exact server version of MongoDB server used

  • the type of deployment (standalone, replica set, or sharded cluster)

  • (for a non-standalone deployment) the read preference used for requests that result in the exception

Thanks,
Stennie

Hi Stennie,

thanks for your response.

I manage to find the problem. I was just trying to create indexes during active mongodb connection.

Everything works fine now.

Thanks