Created
May 7, 2019 15:59
-
-
Save vkarpov15/7fceb4e8437de963c575fb4e3b6ffb86 to your computer and use it in GitHub Desktop.
MongoDB cursor slow train
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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