Docs 菜单

Docs 主页查看和分析数据MongoDB Shell

在脚本中包含外部文件和模块

在此页面上

  • 需要本地文件
  • 需要内置模块
  • 需要 npm 模块

重要

Node.js、模块和 require() 的完整说明 函数超出了本教程的范围。要了解更多信息,请参阅 Node.js 文档。

要在 交互中使用文件和模块,请使用mongosh require() 函数。

mongosh 脚本中,您可以要求:

  • 本地文件

  • 内置 Node.js 模块

  • 外部 (npm) Node.js 模块

您可以在 mongosh 脚本中使用 JavaScript 文件,无需任何额外设置或配置。

注意

mongosh 不执行使用 require() 导入的文件。取而代之的是,mongosh 会将导入文件中的所有内容添加到当前执行范围。

例子

若要包含位于当前工作目录中的名为 test.js 的文件,请使用以下命令之一:

require('./tests.js')
var tests = require('./tests.js')

您可以要求内置 Node.js 模块(例如 fs ),mongosh 任何额外设置或配置。

例子

以下示例创建并执行以下脚本:

  • 连接到在默认端口上运行的本地部署。

  • 使用样本数据填充 myDatabase.employees 集合。

  • 使用 fs 模块将 myDatabase.employees 集合中的文档写入名为 employee.json 的文件。

  1. 创建名为 employee-to-text-file.js 的文件,内容如下:

    const fs = require('fs');
    db = connect('mongodb://localhost/myDatabase');
    db.employees.insertMany( [
    { "name": "Alice", "department": "engineering" },
    { "name": "Bob", "department": "sales" },
    { "name": "Carol", "department": "finance" }
    ] )
    const document = db.employees.findOne();
    fs.writeFileSync('employee.json', JSON.stringify(document));
  2. 要加载并执行 employee-to-text-file.js 文件,请在 mongosh 中运行以下命令:

    load("employee-to-text-file.js")
  3. 要确认数据已写入文件,请打开 employee.json 文件。

您可以要求 Node.js 模块(例如从 npm 下载的模块) )。要使用外部模块,必须安装以下任一模块:

  • 全局安装

  • 在当前工作目录的 node_modules 目录中。

Node.js 模块有两种打包标准。

封装标准
与 require() 一起使用
CommonJS (CJS)
ECMAScript Module (ES模块)

您不能在 mongoshrequire() ES 模块。如果您想使用 ES 模块的功能,请检查是否有可以替代使用的 CommonJS 版本。有关更多信息,请参阅:

提示

您可以使用此构造要求远程 npm 模块:

const localRequire = require('module').createRequire(__filename);)

有关示例,请参阅 index.jsresumetoken 代码段中。

例子

重要

要运行此示例,必须安装 date-fns 模块全局或当前工作目录中的node_modules 目录。

以下示例创建并执行以下脚本:

  • 连接到在默认端口上运行的本地部署。

  • 使用样本数据填充 myDatabase.cakeSales 集合。

  • 使用 date-fns 格式化日期的模块。

  1. 创建名为 date-fns-formatting.js 的文件,内容如下:

    const formatDistance = require('date-fns/formatDistance')
    db = connect('mongodb://localhost/myDatabase');
    db.cakeSales.insertMany( [
    { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
    state: "CA", price: 13, quantity: 120 },
    { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
    state: "WA", price: 14, quantity: 140 },
    { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
    state: "CA", price: 12, quantity: 145 },
    { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
    state: "WA", price: 13, quantity: 104 },
    { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
    state: "CA", price: 41, quantity: 162 },
    { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
    state: "WA", price: 43, quantity: 134 }
    ] )
    const saleDate0 = db.cakeSales.findOne( { _id: 0 } ).orderDate
    const saleDate1 = db.cakeSales.findOne( { _id: 1 } ).orderDate
    const saleDateDistance0 = formatDistance(saleDate0, new Date(), { addSuffix: true })
    const saleDateDistance1 = formatDistance(saleDate1, new Date(), { addSuffix: true })
    print("{ _id: 0 } orderDate was " + saleDateDistance0)
    print("{ _id: 1 } orderDate was " + saleDateDistance1)
  2. 要加载并执行 date-fns-formatting.js 文件,请在 mongosh 中运行以下命令:

    load("date-fns-formatting.js")

    mongosh 输出类似如下内容:

    { _id: 0 } orderDate was over 1 year ago
    { _id: 1 } orderDate was 7 months ago

    输出可能因运行示例的日期不同而异。

← 编写脚本