将 Mongo 集合转储为 JSON 格式

发布于 2024-12-28 20:21:36 字数 74 浏览 3 评论 0原文

有没有办法将 mongo 集合转储为 json 格式?无论是在 shell 上还是使用 java 驱动程序。我正在寻找性能最好的一个。

Is there any way to dump mongo collection into json format? Either on the shell or using java driver.I am looking for the one with best performance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

始终不够爱げ你 2025-01-04 20:21:36

Mongo 包含一个 mongoexport 实用程序(请参阅文档),它可以转储集合。该实用程序使用本机 libmongoclient,可能是最快的方法。

mongoexport -d <database> -c <collection_name>

也有帮助:

-o:将输出写入文件,否则使用标准输出(docs)

--jsonArray:生成一个有效的 json 文档,而不是每行一个 json 对象 (文档)

--pretty :输出格式化的 json (docs)

Mongo includes a mongoexport utility (see docs) which can dump a collection. This utility uses the native libmongoclient and is likely the fastest method.

mongoexport -d <database> -c <collection_name>

Also helpful:

-o: write the output to file, otherwise standard output is used (docs)

--jsonArray: generates a valid json document, instead of one json object per line (docs)

--pretty: outputs formatted json (docs)

拥抱影子 2025-01-04 20:21:36

使用 mongoexport/mongoimport 转储/恢复集合:

导出 JSON 文件

mongoexport --db; --collection <集合名称>; --out output.json

导入 JSON 文件:

mongoimport --db <数据库名称>; --collection <集合名称>; --文件输入.json

警告
mongoimportmongoexport 不能可靠地保留所有丰富的 BSON 数据类型,因为 JSON 只能表示 BSON 支持的类型的子集。因此,使用这些工具导出或导入的数据可能会失去一定程度的保真度。

另外,http://bsonspec.org/

BSON 旨在快速编码和解码。例如,
整数存储为 32(或 64)位整数,因此不需要
被解析为文本或从文本解析。对于小型数据,这比 JSON 使用更多的空间
整数,但解析速度要快得多。

除了紧凑性之外,BSON 还添加了额外的数据类型
在 JSON 中不可用,尤其是 BinData 和 Date 数据类型。

Use mongoexport/mongoimport to dump/restore a collection:

Export JSON File:

mongoexport --db <database-name> --collection <collection-name> --out output.json

Import JSON File:

mongoimport --db <database-name> --collection <collection-name> --file input.json

WARNING
mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity.

Also, http://bsonspec.org/

BSON is designed to be fast to encode and decode. For example,
integers are stored as 32 (or 64) bit integers, so they don't need to
be parsed to and from text. This uses more space than JSON for small
integers, but is much faster to parse.

In addition to compactness, BSON adds additional data types
unavailable in JSON, notably the BinData and Date data types.

执笔绘流年 2025-01-04 20:21:36

这是我的命令供参考:

mongoexport --db AppDB --collection files --pretty --out output.json

在 Windows 7 (MongoDB 3.4) 上,必须将 cmd 移动到 mongod.exemongo.exe 文件所在的位置 =>
C:\MongoDB\Server\3.4\bin 否则它将无法工作,说它无法识别 mongoexport 命令。

Here's mine command for reference:

mongoexport --db AppDB --collection files --pretty --out output.json

On Windows 7 (MongoDB 3.4), one has to move the cmd to the place where mongod.exe and mongo.exe file resides =>
C:\MongoDB\Server\3.4\bin else it won't work saying it does not recongnize mongoexport command.

迷雾森÷林ヴ 2025-01-04 20:21:36

来自蒙戈文档:

mongoexport 实用程序获取集合并导出为 JSON 或 CSV。您可以指定查询的过滤器或要输出的字段列表

在此处阅读更多信息:http: //www.mongodb.org/display/DOCS/mongoexport

From the Mongo documentation:

The mongoexport utility takes a collection and exports to either JSON or CSV. You can specify a filter for the query, or a list of fields to output

Read more here: http://www.mongodb.org/display/DOCS/mongoexport

記憶穿過時間隧道 2025-01-04 20:21:36

这是我编写的一个小节点脚本,用于将特定数据库中的所有集合转储到指定的输出目录...

#!/usr/bin/env node
import { MongoClient } from 'mongodb';
import { spawn } from 'child_process';
import fs from 'fs';

const DB_URI = 'mongodb://0.0.0.0:27017';
const DB_NAME = 'database-name';
const OUTPUT_DIR = 'output-directory';
const client = new MongoClient(DB_URI);

async function run() {
  try {
    await client.connect();

    const db = client.db(DB_NAME);
    const collections = await db.collections();

    if (!fs.existsSync(OUTPUT_DIR)) {
      fs.mkdirSync(OUTPUT_DIR);
    }

    collections.forEach(async (c) => {
      const name = c.collectionName;
      await spawn('mongoexport', [
        '--db',
        DB_NAME,
        '--collection',
        name,
        '--jsonArray',
        '--pretty',
        `--out=./${OUTPUT_DIR}/${name}.json`,
      ]);
    });
  } finally {
    await client.close();
    console.log(`DB Data for ${DB_NAME} has been written to ./${OUTPUT_DIR}/`);
  }
}
run().catch(console.dir);

Here is a little node script that I write to dump all collections in a specific database to the specified output directory...

#!/usr/bin/env node
import { MongoClient } from 'mongodb';
import { spawn } from 'child_process';
import fs from 'fs';

const DB_URI = 'mongodb://0.0.0.0:27017';
const DB_NAME = 'database-name';
const OUTPUT_DIR = 'output-directory';
const client = new MongoClient(DB_URI);

async function run() {
  try {
    await client.connect();

    const db = client.db(DB_NAME);
    const collections = await db.collections();

    if (!fs.existsSync(OUTPUT_DIR)) {
      fs.mkdirSync(OUTPUT_DIR);
    }

    collections.forEach(async (c) => {
      const name = c.collectionName;
      await spawn('mongoexport', [
        '--db',
        DB_NAME,
        '--collection',
        name,
        '--jsonArray',
        '--pretty',
        `--out=./${OUTPUT_DIR}/${name}.json`,
      ]);
    });
  } finally {
    await client.close();
    console.log(`DB Data for ${DB_NAME} has been written to ./${OUTPUT_DIR}/`);
  }
}
run().catch(console.dir);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文