文档菜单
文档首页
/
MongoDB Shell
/

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

本页内容

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

重要

Node.js、模块和 MongoDB 的完整描述require() 函数在本教程中不适用。要了解更多信息,请参阅 Node.js 文档。

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

在您的 mongosh 脚本中,您可以 require

  • 本地文件

  • 内置 Node.js 模块

  • 外部 (npm) Node.js 模块

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

注意

mongosh 不执行使用 require() 导入的文件。相反,mongosh 将导入文件中的所有内容添加到当前执行范围。

示例

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

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

您可以在 mongosh 中使用内置的 Node.js 模块(如 fs)而不需要任何额外的设置或配置。

示例

以下示例创建并执行了一个脚本,

  • 连接到默认端口上运行的本地区域。

  • 用示例数据填充 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. 要从 mongosh 加载并执行 employee-to-text-file.js 文件,请运行以下命令:

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

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

  • 全局安装

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

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

打包标准
与 require() 兼容
CommonJS (CJS)
ECMAScript 模块 (ES Module)

您不能在 mongosh 中使用 require() 来引入 ES 模块。如果您想使用 ES 模块的某些功能,请检查是否有可用的 CommonJS 版本。更多信息请参见

提示

您可以使用以下构造来引入远程 npm 模块

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

例如,请参阅 index.js 中的代码。

示例

重要

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

以下示例创建并执行了一个脚本,

  • 连接到默认端口上运行的本地区域。

  • 将示例数据填充到 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

    您的输出可能会根据您运行示例的日期而有所不同。

返回

编写脚本