Created
January 8, 2024 13:55
-
-
Save dmozgovoi/aa155d60cfb57fb381e35503cde07126 to your computer and use it in GitHub Desktop.
Test example of mongo maxTimeMS option usage
This file contains 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 { MongoClient } = require('mongodb'); | |
// update according to your setup | |
const url = 'mongodb://localhost:27022'; | |
const dbName = 'test'; | |
const collectionName = 'test-timeouts'; | |
const fillData = async () => { | |
const client = new MongoClient(url); | |
const data = Array.from({ length: 10000 }).map(() => ({ | |
name: 'John Doe', | |
age: Math.floor(Math.random() * 100), | |
created: new Date(), | |
content: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.', | |
})); | |
await client.connect(); | |
await client.db(dbName).collection(collectionName).insertMany(data); | |
await client.close(); | |
}; | |
const catchAndLog = async (name, promise) => { | |
try { | |
console.time(name); | |
const result = await promise; | |
console.timeEnd(name); | |
return result; | |
} catch (e) { | |
console.error(`${name} failed:`, e.message); | |
} | |
}; | |
(async () => { | |
// uncomment to fill the collection with 10K docs for the first run | |
// await fillData(); | |
const client = new MongoClient(url); | |
const options = { | |
maxTimeMS: 4, // on 10K docs generated by the fillData() all operations will fail with limit set to <= 4 ms | |
// timeout: false, // useless, changing it from true to false does nothing | |
}; | |
const collection = client.db(dbName).collection(collectionName); | |
// any slow filter will do the job, so I got random timestamp | |
const slowFilter = { created: '2024-01-08T13:18:16.456+00:00' }; | |
await catchAndLog('findOne', collection.findOne(slowFilter, options)); | |
await catchAndLog('find', collection.find(slowFilter, options).toArray()); | |
await catchAndLog('countDocuments', collection.countDocuments(slowFilter, options)); | |
await catchAndLog('updateOne', collection.updateOne(slowFilter, { $set: { updated: 1}}, options)); | |
await catchAndLog('updateMany', collection.updateMany(slowFilter, { $set: { updated: 1}}, options)); | |
await catchAndLog('deleteOne', collection.deleteOne({ ...slowFilter, updatedAt: new Date() }, options)); | |
await catchAndLog('deleteMany', collection.deleteMany({ ...slowFilter, updatedAt: new Date() }, options)); | |
await client.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test collection contained 10K docs.
Outcomes: With 4ms
maxTimeMS
all the operations fail. Even the commands likedeleteOne
andfindOne
. After risingmaxTimeMS
some of the commands pass.Changing the
timeout
setting doesn't affect commands: they fail with 4 msmaxTimeMS
disregarding oftimeout
value