重要
服务器端 JavaScript 已弃用
从MongoDB 8.0开始,服务器端JavaScript函数( $accumulator 、 $function 、 $where )已弃用。 运行这些函数时, MongoDB会记录警告。
从 MongoDB 5.0 开始,Map-reduce 已被弃用。
MongoDB 提供以下命令、方法和运算符,可在服务器端执行 JavaScript 代码:
mapReduceand the correspondingmongoshmethoddb.collection.mapReduce(). For more information, see Map-Reduce.$where运算符,用于计算 JavaScript 表达式或函数以查询文档。$accumulator和$function聚合操作,允许用户定义自定义聚合表达式。
You can also specify a JavaScript file to mongosh to run on the server. For more information, see Running .js files via a mongosh Instance on the Server
注意
MongoDB 中的 JavaScript
尽管这些方法使用 JavaScript,但与 MongoDB 进行的大多数交互并不使用 JavaScript,而是使用采用交互应用程序语言的惯用驱动程序。
如果不需要在服务器端执行 JavaScript 代码,请参阅禁用在服务器端执行 JavaScript。
注意
如果使用 SELinux,任何需要服务器端 JavaScript 的 MongoDB 操作都会导致 segfault 错误。禁用在服务器端执行 JavaScript 描述如何禁用在服务器端执行 JavaScript。
通过 mongosh 实例在服务器上运行 .js 文件
You can specify a JavaScript (.js) file to mongosh to execute the file on the server. 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.
禁用在服务器端执行 JavaScript
您可以禁用 JavaScript 的所有服务器端执行功能:
对于
mongod实例,方法是在命令行中传递--noscripting选项或在配置文件中将security.javascriptEnabled设置为 false。对于
mongos实例,方法是在命令行中传递--noscripting选项或在配置文件中将security.javascriptEnabled设置为 false。
行为
并发
有关任何并发信息,请参阅每种方法或运算符文档。另请参阅并发表格。
不支持的数组与字符串函数
MongoDB6.0 升级用于服务器端JavaScript 、 、$accumulator $function和$where 表达式的内部JavaScript引擎,以及从 MozJS-60 升级到 MozJS-91 。 MozJS- 中存在的几个已弃用的非标准大量和字符串函数已在60 MozJS-91 中删除。