MongoDB是一个开源的NoSQL数据库,它使用BSON(类似于JSON)格式存储数据,在MongoDB中,我们可以使用mongoexport和mongoimport命令来批量导出和导入JSON数据,本文将通过一个实例来介绍如何使用这两个命令将JSON数据批量导出和导入到同一张表。
准备工作
1、安装MongoDB
我们需要在本地计算机上安装MongoDB,可以从官网下载并安装:https://www.mongodb.com/try/download/community
2、启动MongoDB服务
安装完成后,启动MongoDB服务,在Windows系统中,可以在“开始”菜单中找到MongoDB服务;在Linux系统中,可以使用以下命令启动:
sudo service mongod start
3、创建数据库和集合
在MongoDB中,我们需要先创建一个数据库,然后在该数据库中创建一个集合,可以使用以下命令创建数据库和集合:
use myDatabase db.createCollection("myCollection")
使用mongoexport命令导出数据
1、基本语法
mongoexport命令的基本语法如下:
mongoexport db <database> collection <collection> out <output_file> [query <query>] [type <file_type>] [fields <field>] [headerline] [csv] [json] [excel] [tsv] [pretty] [quiet] [limit <limit>] [sort <field>] [upsert] [uri] [ssl] [sslCAFile <ca_file>] [sslCERTFile <cert_file>] [sslKEYFile <key_file>] [authenticationDatabase <auth_db>] [username <username>] [password <password>] [authenticationMechanisms <auth_mechanisms>] [noTypeInference] [ignoreBlanks] [nullString "<value>"] [batchSize <size>] [numInsertionWorkersPerHost <num_workers>] [port <port>] [host <host>] [slaveOk] [oplogReplay] [quiet] [verbose] [progressbar] [humanReadableOutput] [dbQueryTimeoutMS <timeout>] [explain ] [archive ] [gzip ] [ext {}] [jsonArray ] [out <file> ] [queryFile <file> ] [querySelector <selector> ]
2、导出数据示例
假设我们已经创建了一个名为myDatabase的数据库和一个名为myCollection的集合,现在我们要导出这个集合中的所有数据到一个名为data.json的文件中,可以使用以下命令:
mongoexport db myDatabase collection myCollection out data.json
使用mongoimport命令导入数据
1、基本语法
mongoimport命令的基本语法如下:
mongoimport db <database> collection <collection> file <file> [type <file_type>] [headerline] [upsert] [uri] [ssl] [sslCAFile <ca_file>] [sslCERTFile <cert_file>] [sslKEYFile <key_file>] [authenticationDatabase <auth_db>] [username <username>] [password <password>] [authenticationMechanisms <auth_mechanisms>] [ignoreBlanks] [nullString "<a blank value"] [batchSize <size>] [numInsertionWorkers <num_workers>] [port <port>] [host <host>] [slaveOk] [oplogReplay] [quiet] [verbose] [progressbar] [humanReadableOutput] [dbQueryTimeoutMS <timeout>] [upsertFields <fields> ] [continueOnError ] [maxInsertionTimeMs <time_ms> ] [checkKeys ] [dropDups ] forceTableScan query '{ "myField": { "$exists": true } }' queryFile query.json type json out data.json pretty quiet limit 1000000 sort myField upsert authenticationDatabase admin username myUserName password myPassword authenticationMechanisms SCRAMSHA1 sslCAFile /etc/ssl/certs/cacertificates.crt sslCERTFile /etc/ssl/certs/client.crt sslKEYFile /etc/ssl/private/client.key host myHostName port 27017 slaveOk oplogReplay quiet verbose progressbar humanReadableOutput dbQueryTimeoutMS 5000 upsertFields myField,anotherField continueOnError maxInsertionTimeMs 600000000000000000000000000000000000000000000000000000000 forceTableScan query '{ "myField": { "$exists": true } }' queryFile query.json type json out data.json pretty quiet limit 1000000 sort myField upsert authenticationDatabase admin username myUserName password myPassword authenticationMechanisms SCRAMSHA1 sslCAFile /etc/ssl/certs/cacertificates.crt sslCERTFile /etc/ssl/certs/client.crt sslKEYFile /etc/ssl/private/client.key host myHostName port 27017 slaveOk oplogReplay quiet verbose progressbar humanReadableOutput dbQueryTimeoutMS 5000 upsertFields myField,anotherField continueOnError maxInsertionTimeMs 6000000000000000000000000000000000000
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/503905.html