Docs HomeMongoDB Manual

Server-side JavaScript服务器端JavaScript

Overview概述

MongoDB provides the following commands, methods, and operator that perform server-side execution of JavaScript code:MongoDB提供了以下命令、方法和运算符,用于执行JavaScript代码的服务器端执行:

You can also specify a JavaScript file to mongosh to run on the server. 您还可以为mongosh指定一个JavaScript文件,以便在服务器上运行。For more information, see Running .js files via a mongosh Instance on the Server有关更多信息,请参阅通过服务器上的mongosh实例运行.js文件

Note

JavaScript in MongoDB

Although these methods use JavaScript, most interactions with MongoDB do not use JavaScript but use an idiomatic driver in the language of the interacting application.尽管这些方法使用JavaScript,但与MongoDB的大多数交互都不使用JavaScript,而是使用交互应用程序语言中的惯用驱动程序

If you do not need to perform server-side execution of JavaScript code, see Disable Server-Side Execution of JavaScript.如果不需要在服务器端执行JavaScript代码,请参阅禁用JavaScript的服务器端执行

Note

If you are using SELinux, any MongoDB operation that requires server-side JavaScript will result in segfault errors. 如果您使用SELinux,任何需要服务器端JavaScript的MongoDB操作都会导致segfault错误。Disable Server-Side Execution of JavaScript describes how to disable execution of server-side JavaScript.禁用JavaScript的服务器端执行描述了如何禁用服务器端JavaScript的执行。

Running .js files via a mongosh Instance on the Server在服务器上通过mongosh实例运行.js文件

You can specify a JavaScript (.js) file to mongosh to execute the file on the server. 您可以为mongosh指定一个JavaScript(.js)文件,以便在服务器上执行该文件。This is a good technique for performing batch administrative work. When you run mongosh on the server, connecting via the localhost interface, the connection is fast with low latency.这是执行批处理管理工作的好方法。当您在服务器上运行mongosh,通过localhost接口进行连接时,连接速度快,延迟低。

Disable Server-Side Execution of JavaScript禁用JavaScript的服务器端执行

You can disable all server-side execution of JavaScript:您可以禁用JavaScript的所有服务器端执行:

Behavior行为

Concurrency并发

Refer to the individual method or operator documentation for any concurrency information. 有关任何并发信息,请参阅单个方法或运算符文档。See also the concurrency table.另请参阅并发表

Unsupported Array and String Functions不支持的数组和字符串函数

MongoDB 6.0 upgrades the internal JavaScript engine used for server-side JavaScript, $accumulator, $function, and $where expressions and from MozJS-60 to MozJS-91. MongoDB 6.0升级了用于服务器端JavaScript$accumulator$function$where表达式的内部JavaScript引擎,并从MozJS-60升级到MozJS-91。Several deprecated, non-standard array and string functions that existed in MozJS-60 are removed in MozJS-91.MozJS-60中存在的一些不推荐使用的非标准数组和字符串函数在MozJS-91中被删除。

For the complete list of removed array and string functions, see the 6.0 compatibility notes.有关已删除的数组和字符串函数的完整列表,请参阅6.0兼容性说明