Skip to content

Instantly share code, notes, and snippets.

@vkarpov15
Created May 7, 2019 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkarpov15/7fceb4e8437de963c575fb4e3b6ffb86 to your computer and use it in GitHub Desktop.
Save vkarpov15/7fceb4e8437de963c575fb4e3b6ffb86 to your computer and use it in GitHub Desktop.
MongoDB cursor slow train
const mongodb = require('mongodb')
run().catch(error => console.log(error));
async function run() {
const db = await mongodb.MongoClient.
connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
poolSize: 1 // Only 1 operation can run at a time
}).
then(client => client.db());
await db.dropDatabase();
await db.collection('Test').insertMany([{ test: 1 }, { test: 2 }]);
const cursor = db.collection('Test').find({ $where: 'sleep(1000) || true' });
// If you uncomment the below line, the `findOne()` query will take about 1000ms,
// instead of 2000ms
// cursor.batchSize(1);
cursor.next();
const startTime = Date.now();
await db.collection('Test').findOne();
// "Executed query in 2013 ms"
console.log('Executed query in', Date.now() - startTime, 'ms');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment