pow-mongodb-fixtures icon indicating copy to clipboard operation
pow-mongodb-fixtures copied to clipboard

Switches from drop to removeMany

Open jperkelens opened this issue 8 years ago • 6 comments

It seems like many people (myself included) were receiving sporadic errors about mongodb background operations. This is because the drop command also clears indices for the collection, forcing mongodb to reindex the next time the collection is used. Switching the command from drop to removeMany keeps the indices intact and prevents this from happening.

jperkelens avatar Mar 18 '16 20:03 jperkelens

What kind of errors are you seeing?

powmedia avatar Mar 20 '16 14:03 powmedia

Here's an example stack trace:

Unhandled rejection MongoError: exception: cannot perform operation: a background operation is currently running for collection** users
    at Function.MongoError.create (/somepath/node_modules/pow-mongodb-fixtures/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)
    at /somepath/node_modules/pow-mongodb-fixtures/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:793:66
    at bound (domain.js:254:14)
    at runBound (domain.js:267:12)
    at Callbacks.emit (.../node_modules/pow-mongodb-fixtures/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:94:3)
    at null.messageHandler (/somepath/node_modules/pow-mongodb-fixtures/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:235:23)
    at Socket.<anonymous> (/somepath/node_modules/pow-mongodb-fixtures/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:294:20)
    at Socket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
    at Socket.Readable.push (_stream_readable.js:126:10)
    at TCP.onread (net.js:538:20)

jperkelens avatar Mar 21 '16 13:03 jperkelens

What are the implications of using removeMany() over drop() when it comes to indexes? The MongoDB docs say that drop() removes the indices on a collection as well. Do you know what happens with them when using removeMany instead?

On Mon, Mar 21, 2016 at 1:50 PM Jan Paul Erkelens [email protected] wrote:

Here's an example stack trace:

Unhandled rejection MongoError: exception: cannot perform operation: a background operation is currently running for collection** users at Function.MongoError.create (/somepath/node_modules/pow-mongodb-fixtures/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11) at /somepath/node_modules/pow-mongodb-fixtures/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:793:66 at bound (domain.js:254:14) at runBound (domain.js:267:12) at Callbacks.emit (.../node_modules/pow-mongodb-fixtures/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:94:3) at null.messageHandler (/somepath/node_modules/pow-mongodb-fixtures/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:235:23) at Socket. (/somepath/node_modules/pow-mongodb-fixtures/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:294:20) at Socket.emit (events.js:107:17) at readableAddChunk (_stream_readable.js:163:16) at Socket.Readable.push (_stream_readable.js:126:10) at TCP.onread (net.js:538:20)

— You are receiving this because you commented.

Reply to this email directly or view it on GitHub https://github.com/powmedia/pow-mongodb-fixtures/pull/31#issuecomment-199290116

powmedia avatar Mar 21 '16 16:03 powmedia

removeMany() keeps the indices intact, however, since the document is being removed the entry in the index should be removed as well and should not grow the index indefinitely.

Since its common practice to clear/load your data in a beforeEach test setup, I tended to encounter this error 20-35% of the time I ran my tests. If you are running a test suite multiple times, your collection indices are unlikely to change. If you'd like to keep a command in this library that does drop collections I recommend creating a new command (drop perhaps?) that can be called at the end of the test suite.

jperkelens avatar Mar 21 '16 16:03 jperkelens

Just wanted to check in to make sure my last comment answered your questions.

jperkelens avatar Mar 24 '16 15:03 jperkelens

@jperkelens The repo owner isn't around anymore...

I've forked the project and published an own package: https://www.npmjs.com/package/mongodb_fixtures

Feel free to use it 😄

BorntraegerMarc avatar Jan 03 '17 21:01 BorntraegerMarc