mongoexport(v2.6)使用 # mongoexport --help Export MongoDB data to CSV, TSV or JSON files. Options: --help produce help message -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv) --quiet silence all non error diagnostic messages --version print the program's version and exit -h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets) --port arg server port. Can also use --host hostname:port --ipv6 enable IPv6 support (disabled by default) -u [ --username ] arg username -p [ --password ] arg password --authenticationDatabase arg user source (defaults to dbname) --authenticationMechanism arg (=MONGODB-CR) authentication mechanism --gssapiServiceName arg (=mongodb) Service name to use when authenticating using GSSAPI/Kerberos --gssapiHostName arg Remote host name to use for purpose of GSSAPI/Kerberos authentication --dbpath arg directly access mongod database files in the given path, instead of connecting to a mongod server - needs to lock the data directory, so cannot be used if a mongod is currently accessing the same path --directoryperdb each db is in a separate directory (relevant only if dbpath specified) --journal enable journaling (relevant only if dbpath specified) -d [ --db ] arg database to use -c [ --collection ] arg collection to use (some commands) -f [ --fields ] arg comma separated list of field names e.g. -f name,age --fieldFile arg file with field names - 1 per line -q [ --query ] arg query filter, as a JSON string, e.g., '{x:{$gt:1}}' --csv export to csv instead of json -o [ --out ] arg output file; if not specified, stdout is used --jsonArray output to a json array rather than one object per line -k [ --slaveOk ] arg (=1) use secondaries for export if available, default true --forceTableScan force a table scan (do not use $snapshot) --skip arg (=0) documents to skip, default 0 --limit arg (=0) limit the numbers of documents returned, default all --sort arg sort order, as a JSON string, e.g., '{x:1}' 1. 插入測試數據 > use mydb; switched to db mydb > db.foo.insert({student : {name : "caoqing", age : 5}}); WriteResult({ "nInserted" : 1 }) > db.foo.insert({student : {name : "xiaobao", age : 10}}); WriteResult({ "nInserted" : 1 }) > db.foo.insert({student : {name : "caoqing", age : 50}}); WriteResult({ "nInserted" : 1 }) > db.foo.insert({student : {name : "caoqing", age : 500}}); WriteResult({ "nInserted" : 1 }) 2. 查詢結果 # mongo --eval 'db.getSiblingDB("mydb").foo.find({"student.name" : "caoqing"}).forEach(function(x) {printjson(x)})' MongoDB shell version: 2.6.1 connecting to: test { "_id" : ObjectId("53bdf37476f285fe0551b720"), "student" : { "name" : "caoqing", "age" : 5 } } { "_id" : ObjectId("53bdf3b37e391662b46ca278"), "student" : { "name" : "caoqing", "age" : 50 } } { "_id" : ObjectId("53bdf3b57e391662b46ca279"), "student" : { "name" : "caoqing", "age" : 500 } } 3. mongoexport導出數據測試 3.1 加入查詢條件 # mongoexport -dmydb -c foo -q '{"student.name" : "caoqing"}' -o 1 connected to: 127.0.0.1 exported 3 records # cat 1 { "_id" : { "$oid" : "53bdf37476f285fe0551b720" }, "student" : { "name" : "caoqing", "age" : 5 } } { "_id" : { "$oid" : "53bdf3b37e391662b46ca278" }, "student" : { "name" : "caoqing", "age" : 50 } } { "_id" : { "$oid" : "53bdf3b57e391662b46ca279" }, "student" : { "name" : "caoqing", "age" : 500 } } 3.2 加入查詢條件並限定返回數目爲2 # mongoexport -dmydb -c foo -q '{"student.name" : "caoqing"}' -o 1 --limit 2 connected to: 127.0.0.1 exported 2 records # cat 1 { "_id" : { "$oid" : "53bdf37476f285fe0551b720" }, "student" : { "name" : "caoqing", "age" : 5 } } { "_id" : { "$oid" : "53bdf3b37e391662b46ca278" }, "student" : { "name" : "caoqing", "age" : 50 } } 3.3 加入查詢條件並忽略數目1限定數目爲2 # mongoexport -dmydb -c foo -q '{"student.name" : "caoqing"}' -o 1 --limit 2 --skip 1 connected to: 127.0.0.1 exported 2 records # cat 1 { "_id" : { "$oid" : "53bdf3b37e391662b46ca278" }, "student" : { "name" : "caoqing", "age" : 50 } } { "_id" : { "$oid" : "53bdf3b57e391662b46ca279" }, "student" : { "name" : "caoqing", "age" : 500 } } 3.3 加入查詢條件,並按照年齡排序,忽略數目1限定數目爲2 # mongoexport -dmydb -c foo -q '{"student.name" : "caoqing"}' -o 1 --limit 2 --skip 1 --sort '{"student.age" : -1}' connected to: 127.0.0.1 exported 2 records # cat 1 { "_id" : { "$oid" : "53bdf3b37e391662b46ca278" }, "student" : { "name" : "caoqing", "age" : 50 } } { "_id" : { "$oid" : "53bdf37476f285fe0551b720" }, "student" : { "name" : "caoqing", "age" : 5 } } 3.4 加入查詢條件,並按照年齡排序,忽略數目1限定數目爲2,而且輸出爲json數組 # mongoexport -dmydb -c foo -q '{"student.name" : "caoqing"}' -o 1 --limit 2 --skip 1 --sort '{"student.age" : -1}' --jsonArray connected to: 127.0.0.1 exported 2 records # cat 1 [{ "_id" : { "$oid" : "53bdf3b37e391662b46ca278" }, "student" : { "name" : "caoqing", "age" : 50 } },{ "_id" : { "$oid" : "53bdf37476f285fe0551b720" }, "student" : { "name" : "caoqing", "age" : 5 } }] 3.5 導出特定域值,會返回子文檔的全部域值,而不僅是指定的域 # mongoexport -dmydb -c foo -q '{"student.name" : "caoqing"}' -f student.name,_id -o 2 connected to: 127.0.0.1 exported 3 records [root@mongo02 mmsdb]# cat 2 { "_id" : { "$oid" : "53bdf37476f285fe0551b720" }, "student" : { "name" : "caoqing", "age" : 5 } } { "_id" : { "$oid" : "53bdf3b37e391662b46ca278" }, "student" : { "name" : "caoqing", "age" : 50 } } { "_id" : { "$oid" : "53bdf3b57e391662b46ca279" }, "student" : { "name" : "caoqing", "age" : 500 } } 3.6 導出特定域值爲csv格式 # mongoexport -dmydb -c foo -q '{"student.name" : "caoqing"}' --csv -f student.age,_id -o 2 connected to: 127.0.0.1 exported 3 records [root@mongo02 mmsdb]# cat 2 student.age,_id 5.0,ObjectID(53bdf37476f285fe0551b720) 50.0,ObjectID(53bdf3b37e391662b46ca278) 500.0,ObjectID(53bdf3b57e391662b46ca279) 3.7 使用指定的域值文件,只支持csv格式導出 cat >> fieldfile.txt << EOF student.name student.age _id EOF # mongoexport -dmydb -c foo -q '{"student.name" : "caoqing"}' --fieldFile fieldfile.txt --csv -o 2 connected to: 127.0.0.1 exported 3 records [root@mongo02 mmsdb]# cat 2 student.name,student.age,_id "caoqing",5.0,ObjectID(53bdf37476f285fe0551b720) "caoqing",50.0,ObjectID(53bdf3b37e391662b46ca278) "caoqing",500.0,ObjectID(53bdf3b57e391662b46ca279) 3.8 使用指定的域值文件,而且從文件中讀取查詢條件 echo {"student.name":"caoqing","student.age":{$gte:30}} > query.txt # mongoexport -d mydb -c foo --query $(cat query.txt) -o 1 connected to: 127.0.0.1 exported 2 records [root@mongo02 mmsdb]# cat 1 { "_id" : { "$oid" : "53bdf3b37e391662b46ca278" }, "student" : { "name" : "caoqing", "age" : 50 } } { "_id" : { "$oid" : "53bdf3b57e391662b46ca279" }, "student" : { "name" : "caoqing", "age" : 500 } }