此处已有一个MongoDB数据库,其中有个5万多条数据,需要导出,给别人本地调试使用。
mongodb 导出数据
此处看到很多人写导出文件名后缀是dat?
所以问题变为:
mongodb导出数据时 文件名后缀应该用什么?db?dat?json?
mongoexport file name suffix
“
–type <string>
Default: json
New in version 3.0.
Specifies the file type to export. Specify csv for CSV format or json for JSON format.
If you specify csv, then you must also use either the –fields or the –fieldFile option to declare the fields to export from the collection.
–out <file>, -o <file>¶
Specifies a file to write the export to. If you do not specify a file name, the mongoexport writes data to standard output (e.g. stdout).”
可见:
如果不指定–type和–out,则:
默认输出json数据格式,且输出到console中。
所以此处:
–out的文件名后缀,则应该写成:默认的json格式的.json
所以此处去:
➜ output git:(master) ✗ mongodump -h 127.0.0.1 -P 27017 -d Scholastic -o storybook.scholastic.json 2018-10-23T10:48:17.368+0800 error parsing command line options: unknown option "P" 2018-10-23T10:48:17.369+0800 try 'mongodump --help' for more information
结果去看看help
➜ output git:(master) ✗ mongodump --help Usage: mongodump <options> Export the content of a running server into .bson files. Specify a database with -d and a collection with -c to only dump that database or collection. See http://docs.mongodb.org/manual/reference/program/mongodump/ for more information. general options: --help print usage --version print the tool version and exit verbosity options: -v, --verbose=<level> more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N) --quiet hide all log output connection options: -h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets) --port=<port> server port (can also use --host hostname:port) ssl options: --ssl connect to a mongod or mongos that has ssl enabled --sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority --sslPEMKeyFile=<filename> the .pem file containing the certificate and key --sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary --sslCRLFile=<filename> the .pem file containing the certificate revocation list --sslAllowInvalidCertificates bypass the validation for server certificates --sslAllowInvalidHostnames bypass the validation for server name --sslFIPSMode use FIPS mode of the installed openssl library authentication options: -u, --username=<username> username for authentication -p, --password=<password> password for authentication --authenticationDatabase=<database-name> database that holds the user's credentials --authenticationMechanism=<mechanism> authentication mechanism to use namespace options: -d, --db=<database-name> database to use -c, --collection=<collection-name> collection to use uri options: --uri=mongodb-uri mongodb uri connection string query options: -q, --query= query filter, as a JSON string, e.g., '{x:{$gt:1}}' --queryFile= path to a file containing a query filter (JSON) --readPreference=<string>|<json> specify either a preference name or a preference json object --forceTableScan force a table scan output options: -o, --out=<directory-path> output directory, or '-' for stdout (defaults to 'dump') --gzip compress archive our collection output with Gzip --repair try to recover documents from damaged data files (not supported by all storage engines) --oplog use oplog for taking a point-in-time snapshot --archive=<file-path> dump as an archive to the specified path. If flag is specified without a value, archive is written to stdout --dumpDbUsersAndRoles dump user and role definitions for the specified database --excludeCollection=<collection-name> collection to exclude from the dump (may be specified multiple times to exclude additional collections) --excludeCollectionsWithPrefix=<collection-prefix> exclude all collections from the dump that have the given prefix (may be specified multiple times to exclude additional prefixes) -j, --numParallelCollections= number of collections to dump in parallel (4 by default) (default: 4) --viewsAsCollections dump views as normal collections with their produced data, omitting standard collections ➜ output git:(master) ✗
发现–port缩写不是-P,就没有缩写。
所以写成:
➜ output git:(master) ✗ mongodump -h 127.0.0.1 --port 27017 -d Scholastic -o storybook.scholastic.json 2018-10-23T10:49:25.416+0800 writing Scholastic.Storybook to 2018-10-23T10:49:26.397+0800 done dumping Scholastic.Storybook (51785 documents) ➜ output git:(master) ✗ ll -h total 0 drwxr-xr-x 3 crifan staff 96B 10 23 10:49 storybook.scholastic.json
但是导出数据只有96B,有问题啊。
结果去Finder中看到原来是文件夹:
那么换成直接导出此处要的数据库吧
➜ output git:(master) ✗ mongodump -h 127.0.0.1 --port 27017 -d Scholastic -c Storybook -o storybook_scholastic.json 2018-10-23T10:51:29.953+0800 writing Scholastic.Storybook to 2018-10-23T10:51:30.753+0800 done dumping Scholastic.Storybook (51785 documents) ➜ output git:(master) ✗ ll -lh total 0 drwxr-xr-x 3 crifan staff 96B 10 23 10:49 storybook.scholastic.json drwxr-xr-x 3 crifan staff 96B 10 23 10:51 storybook_scholastic.json
结果还是文件夹。
才注意到:
output options: -o, --out=<directory-path> output directory, or '-' for stdout (defaults to 'dump')
–out不是文件名,而是文件路径
所以试试:
➜ output git:(master) ✗ mongodump -h 127.0.0.1 --port 27017 -d Scholastic -c Storybook -o ./storybook_scholastic.json 2018-10-23T10:52:40.523+0800 writing Scholastic.Storybook to 2018-10-23T10:52:41.075+0800 done dumping Scholastic.Storybook (51785 documents) ➜ output git:(master) ✗ ll -lh total 0 drwxr-xr-x 3 crifan staff 96B 10 23 10:49 storybook.scholastic.json drwxr-xr-x 3 crifan staff 96B 10 23 10:51 storybook_scholastic.json
问题依旧。
看来:
想要输出到 当前文件夹中指定json文件名,则无法实现
只能:指定输出的目录,然后系统自动生成json,文件名则是你的collection的文件名:
算了,那就写成:
➜ output git:(master) ✗ mongodump -h 127.0.0.1 --port 27017 -d Scholastic -c Storybook -o . 2018-10-23T10:54:06.679+0800 writing Scholastic.Storybook to 2018-10-23T10:54:07.457+0800 done dumping Scholastic.Storybook (51785 documents) ➜ output git:(master) ✗ ll -lha total 0 drwxr-xr-x 5 crifan staff 160B 10 23 10:54 . drwxr-xr-x 10 crifan staff 320B 10 23 10:43 .. drwxr-xr-x 4 crifan staff 128B 10 23 10:54 Scholastic drwxr-xr-x 3 crifan staff 96B 10 23 10:49 storybook.scholastic.json drwxr-xr-x 3 crifan staff 96B 10 23 10:51 storybook_scholastic.json ➜ output git:(master) ✗ tree Scholastic Scholastic ├── Storybook.bson └── Storybook.metadata.json 0 directories, 2 files ➜ output git:(master) ✗ ll -lh Scholastic/Storybook.bson -rw-r--r-- 1 crifan staff 60M 10 23 10:54 Scholastic/Storybook.bson ➜ output git:(master) ✗ ll -lh Scholastic/ total 122408 -rw-r--r-- 1 crifan staff 60M 10 23 10:54 Storybook.bson -rw-r--r-- 1 crifan staff 134B 10 23 10:54 Storybook.metadata.json
结果
所以可以了。
【总结】
此处用:
mongodump -h 127.0.0.1 --port 27017 -d Scholastic -c Storybook -o .
将:
- -d=–database:数据库 Scholastic
- -c=–collection:集合=表,Storybook
中的数据,输出为:
- –type:默认为json
- -o==–out:. 表示 当前文件夹
最终得到了输出的:
Scholastic文件夹中的:
- 数据文件:Storybook.bson,60M
- 二进制格式
- 元数据:Storybook.metadata.json,130多B
转载请注明:在路上 » 【已解决】从MongoDB数据库中导出数据