skip to content
Alvin Lucillo

Set maximum time of a query

/ 1 min read

One of the performance guardrails to ensure a query doesn’t spend too much time on a queury is to set maxTimeMS, which is the maximum amount of time the server is allowed to spend in a given query. Since the time set in the aggregate operation is too small, the server threw MaxTimeMSExpired.

db.persons.aggregate([
{
    $match: {
    first_name: {
        $regex: "a"
    }
    }
}],
    {maxTimeMS: 1}
)
MongoServerError[MaxTimeMSExpired]: operation exceeded time limit