需求介紹:將mongodb中的數據以文件的方式導出:json或cvs格式
mongo 提供了mongoexport的工具,可以實現將庫中的數據以json或cvs的格式輸出到文件中。mongoexport位於mongo安裝位置中的bin/目錄下。
mongoexport具體用法如下所示:
1. 使用help查看參數說明
E:\data λ mongoexport --help Usage: mongoexport <options> Export data from MongoDB in CSV or JSON format. See http://docs.mongodb.org/manual/reference/program/mongoexport/ for more information. general options: /help print usage /version print the tool version and exit verbosity options: /v, /verbose more detailed log output (include multiple times for more verbosity, e.g. -vvvvv) /quiet hide all log output connection options: /h, /host: mongodb host to connect to (setname/host1,host2 for replica sets) /port: server port (can also use --host hostname:port) authentication options: /u, /username: username for authentication /p, /password: password for authentication /authenticationDatabase: database that holds the user's credentials /authenticationMechanism: authentication mechanism to use namespace options: /d, /db: database to use /c, /collection: collection to use output options: /f, /fields: comma separated list of field names (required for exporting CSV) e.g. -f "name,age" /fieldFile: file with field names - 1 per line /type: the output format, either json or csv (defaults to 'json') /o, /out: output file; if not specified, stdout is used /jsonArray output to a JSON array rather than one object per line /pretty output JSON formatted to be human-readable querying options: /q, /query: query filter, as a JSON string, e.g., '{x:{$gt:1}}' /k, /slaveOk allow secondary reads if available (default true) /forceTableScan force a table scan (do not use $snapshot) /skip: number of documents to skip /limit: limit the number of documents to export /sort: sort order, as a JSON string, e.g. '{x:1}'
2. 用例說明:
將info庫中student的所有信息以json格式導出到student_json.dat數據文件中
mongoexport -h 127.0.0.1 -u root -p 12345 -d info -c student --type=json -o E:\data\student_json.dat
輸出:
{ "id": 123, "name": "張三", "age": 12 } { "id": 124, "name": "李四", "age": 15 }
將info庫中student的id,name信息以json格式導出到student_json.dat數據文件中,並且限定“行數”是1
mongoexport -h 127.0.0.1 -u root -p 12345 -d info -c student --type=json -f id,name --limit=1 -o E:\data\student_json.dat
輸出:
{ "id": 1, "name": "張三" }
將info庫中student的所有信息以cvs格式導出到student_cvs.dat數據文件中
mongoexport -h 127.0.0.1 -u root -p 12345 -d info -c student --type=cvs -o E:\data\student_cvs.dat
輸出:
123,"張三",12 124,"李四",15
將info庫student“表”的name=張三的信息以cvs格式導出到student_cvs.dat數據文件中
mongoexport -h 127.0.0.1 -u root -p 12345 -d info -c student --type=cvs -q{"name":"張三"} -o E:\data\student_cvs.dat
輸出:
123,"張三",12
注意:
a. --type=json 只是控制每一條“記錄”是json格式,而整體的輸出文件不是json 。如果想要控制整體的文件數據格式是json數組,則需要使用--jsonArray 參數控制
擴展:
導入工具mongoimport
Mongodb中的mongoimport工具可以把一個特定格式文件中的內容導入到指定的collection中。該工具可以導入JSON格式數據,也可以導入CSV格式數據。具體使用如下所示:
E:\data λ mongoimport --help Usage: mongoimport <options> <file> Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin. See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information. general options: /help print usage /version print the tool version and exit verbosity options: /v, /verbose more detailed log output (include multiple times for more verbosity, e.g. -vvvvv) /quiet hide all log output connection options: /h, /host: mongodb host to connect to (setname/host1,host2 for replica sets) /port: server port (can also use --host hostname:port) authentication options: /u, /username: username for authentication /p, /password: password for authentication /authenticationDatabase: database that holds the user's credentials /authenticationMechanism: authentication mechanism to use namespace options: /d, /db: database to use /c, /collection: collection to use input options: /f, /fields: comma separated list of field names, e.g. -f name,age /fieldFile: file with field names - 1 per line /file: file to import from; if not specified, stdin is used /headerline use first line in input source as the field list (CSV and TSV only) /jsonArray treat input source as a JSON array /type: input format to import: json, csv, or tsv (defaults to 'json') ingest options: /drop drop collection before inserting documents /ignoreBlanks ignore fields with empty values in CSV and TSV /maintainInsertionOrder insert documents in the order of their appearance in the input source /j, /numInsertionWorkers: number of insert operations to run concurrently (defaults to 1) /stopOnError stop importing at first insert/upsert error /upsert insert or update objects that already exist /upsertFields: comma-separated fields for the query part of the upsert /writeConcern: write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}' (defaults to 'majority')
參數說明:
-h:指明數據庫宿主機的IP
-u:指明數據庫的用戶名
-p:指明數據庫的密碼
-d:指明數據庫的名字
-c:指明collection的名字
-f:指明要導入那些列
示例:先刪除students中的數據,並驗證
> db.students.remove()
> db.students.find()
>
然后再導入上面導出的students.dat文件中的內容
mongoimport -d test -c students students.dat connected to: 127.0.0.1 imported 9 objects
參數說明:
-d:指明數據庫名,本例中為test
-c:指明collection名,本例中為students
students.dat:導入的文件名
查詢students集合中的數據
> db.students.find() { "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" } { "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" } { "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" } { "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" } { "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" } { "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" } { "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" } { "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" } { "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" } >
證明數據導入成功
上面演示的是導入JSON格式的文件中的內容,如果要導入CSV格式文件中的內容,則需要通過--type參數指定導入格式,具體如下所示:
先刪除數據
> db.students.remove() > db.students.find() >
再導入之前導出的students_csv.dat文件
mongoimport -d test -c students --type csv --headerline --file students_csv.dat connected to: 127.0.0.1 imported 10 objects
參數說明:
-type:指明要導入的文件格式
-headerline:指明第一行是列名,不需要導入
-file:指明要導入的文件
查詢students集合,驗證導入是否成功:
> db.students.find() { "_id" : ObjectId("503266029355c632cd118ad8"), "classid" : 1, "name" : "kobe", "age" : 20 } { "_id" : ObjectId("503266029355c632cd118ad9"), "classid" : 1, "name" : "nash", "age" : 23 } { "_id" : ObjectId("503266029355c632cd118ada"), "classid" : 2, "name" : "james", "age" : 18 } { "_id" : ObjectId("503266029355c632cd118adb"), "classid" : 2, "name" : "wade", "age" : 19 } { "_id" : ObjectId("503266029355c632cd118adc"), "classid" : 2, "name" : "bosh", "age" : 19 } { "_id" : ObjectId("503266029355c632cd118add"), "classid" : 2, "name" : "allen", "age" : 25 } { "_id" : ObjectId("503266029355c632cd118ade"), "classid" : 1, "name" : "howard", "age" : 19 } { "_id" : ObjectId("503266029355c632cd118adf"), "classid" : 1, "name" : "paul", "age" : 22 } { "_id" : ObjectId("503266029355c632cd118ae0"), "classid" : 2, "name" : "shane", "age" : 24 } >
說明已經導入成功