Database Manual / CRUD Operations / Query

Query Timeouts查询超时

You can specify a timeout for read operations to complete. If a query exceeds the specified time limit, MongoDB stops the query and the query does not return any results.您可以指定完成读取操作的超时时间。如果查询超过指定的时间限制,MongoDB将停止查询,查询不返回任何结果。

To prevent ongoing queries from negatively impacting deployment performance for long periods of time, specify a suitable query timeout for your application.为了防止正在进行的查询长时间对部署性能产生负面影响,请为应用程序指定合适的查询超时。

For details on how MongoDB stops queries that exceed a specified timeout, see cursor.maxTimeMS Behaviors.有关MongoDB如何停止超过指定超时的查询的详细信息,请参阅cursor.maxTimeMS行为

Specify a Time Limit for Queries指定查询的时间限制

To specify a time limit for a query, perform one of these actions:要为查询指定时间限制,请执行以下操作之一:

  • Specify the maxTimeMS() option for a query. The maxTimeMS option lets you specify a query timeout at the operation level, meaning you can specify different time limits for different queries.为查询指定maxTimeMS()选项。maxTimeMS选项允许您在操作级别指定查询超时,这意味着您可以为不同的查询指定不同的时间限制。
  • Specify a global default time limit for all queries. The defaultMaxTimeMS cluster parameter specifies a default time limit for individual read operations to complete, and applies to all queries that do not include the maxTimeMS() option. 为所有查询指定全局默认时间限制。defaultMaxTimeMS集群参数指定了完成单个读取操作的默认时间限制,并适用于不包括maxTimeMS()选项的所有查询。If a query specifies a maxTimeMS() option, that value overrides the defaultMaxTimeMS value.如果查询指定了maxTimeMS()选项,则该值将覆盖defaultMaxTimeMS值。

Learn More了解更多