Server-side JavaScript服务器端JavaScript
On this page
Overview概述
MongoDB provides the following commands, methods, and operator that perform server-side execution of JavaScript code:MongoDB提供了以下命令、方法和运算符,用于执行JavaScript代码的服务器端执行:
-
mapReduce
and the correspondingmongosh
methoddb.collection.mapReduce()
.mapReduce
和相应的mongosh
方法db.collection.mapReduce()
。For more information, see Map-Reduce.有关详细信息,请参阅Map-Reduce。 -
$where
operator that evaluates a JavaScript expression or a function in order to query for documents.运算符,用于评估JavaScript表达式或函数以查询文档。 -
$accumulator
and和$function
aggregation operations that allows users to define custom aggregation expressions.聚合操作,允许用户定义自定义聚合表达式。
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 Servermongosh
实例运行.js
文件
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的服务器端执行。
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
文件
.js
files via a mongosh
Instance on the ServerYou 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的所有服务器端执行:
-
For a对于mongod
instance by passing the--noscripting
option on the command line or settingsecurity.javascriptEnabled
to false in the configuration file.mongod
实例,通过在命令行上传递--noscripting
选项或在配置文件中将security.javascriptEnabled
设置为false
。 -
Starting in MongoDB 4.4, for a从MongoDB 4.4开始,对于mongos
instance by passing the--noscripting
option on the command line or settingsecurity.javascriptEnabled
to false in the configuration file.mongos
实例,通过在命令行上传递--noscripting
选项或在配置文件中将security.javascriptEnabled
设置为false
。
In earlier versions, MongoDB does not allow JavaScript execution on在早期版本中,MongoDB不允许在mongos
instances.mongos
实例上执行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, MongoDB 6.0升级了用于服务器端JavaScript、$accumulator
, $function
, and $where
expressions and from MozJS-60 to MozJS-91. $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兼容性说明。