Mongodb 数据导出CSV Mongoexport的使用

本文详细介绍如何使用Mongoexport工具导出MongoDB数据至CSV文件。包括指定数据库、集合、字段及查询条件等关键步骤,适用于Windows环境,同时指出在Linux环境下执行的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mongoexport 使用

样板语句:

mongoexport -h ip地址 --port 端口 -u 用户名-p 密码 -d 数据库名-c collection名 -f 字段1,字段2,字段3 --query “{date:$gte:Date(1540425600000),$lte:Date(1545782399000)},其他条件}” --type=csv --out example.csv

此实用windows下查询,提交运维或者linux执行需要将 --query中的双引号改为单引号,其中时间为格林乔治时间对应的时间戳,根据mongodb ISODate(时间).valueof 获取,或者使用js获取。其中用户名,密码可省略。

### 将 MongoDB 数据导出CSV 文件的方法 #### 使用 Java 实现 MongoDBCSV 的转换 为了通过 Java 程序将 MongoDB 中的数据导出CSV 文件,可以采用如下方法: 定义一个 Java 方法来处理整个流程。此过程涉及建立与 MongoDB 的连接、查询所需数据并将其写入 CSV 文件。 ```java import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class MongoToCsvExporter { public static void main(String[] args) { try (MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017")) { MongoDatabase database = mongoClient.getDatabase("student"); MongoCollection<Document> collection = database.getCollection("score"); List<String[]> rows = new ArrayList<>(); for (Document doc : collection.find()) { String name = doc.getString("name"); int age = doc.getInteger("age"); String province = doc.getString("province"); Document subjectScores = doc.getObject("subject", Document.class); double chineseScore = subjectScores.getDouble("Chinese"); double englishScore = subjectScores.getDouble("English"); double mathScore = subjectScores.getDouble("Math"); double chemistryScore = subjectScores.getDouble("Chemistry"); rows.add(new String[]{name, Integer.toString(age), province, Double.toString(chineseScore), Double.toString(englishScore), Double.toString(mathScore), Double.toString(chemistryScore)}); } writeRowsToFile(rows, "D:\\tmp\\exp_data.csv"); } } private static void writeRowsToFile(List<String[]> rows, String filePath) throws IOException { try (FileWriter writer = new FileWriter(filePath)) { // Write header line first. writer.append("Name,Age,Province,Chinese Score,English Score,Math Score,Chemistry Score\n"); for (String[] row : rows) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < row.length; ++i) { if (i != 0) sb.append(','); sb.append(row[i]); } writer.append(sb).append('\n'); } } } } ``` 上述代码展示了如何利用 Java 和 MongoDB 官方驱动程序读取 `student` 数据库下的 `score` 集合中的文档,并按照指定格式保存到本地磁盘上的 CSV 文件中[^1]。 #### 基于命令行工具 `mongoexport` 导出 CSV 文件 另一种更简便的方式是借助官方提供的命令行工具——`mongoexport` 来完成这项工作。下面是一个具体的例子说明怎样做: ```bash mongoexport \ --host localhost \ --type csv \ --fields name,age,province,'subject.Chinese','subject.English','subject.Math','subject.Chemistry' \ --out D:\tmp\exp_data.csv ``` 这条命令会从名为 `student` 的数据库里选取 `score` 表格里的记录,只保留特定字段并将它们以逗号分隔的形式存储在一个新的 CSV 文档内[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值