【示例】mongoexport导出某一时间段内的数据
MongoDB的官方文档做出了很详实的说明:[mongoexport
参数说明及示例]^1。
在此将该示例作为笔记进行记录。
mongoexport命令说明
可使用以下命令对参数项进行查看
mongoexport --help
显示结果如下:
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)
authentication options:
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
--authenticationDatabase=<database-name> database that holds the user's
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') (default:
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
--noHeaderLine export CSV data without a list of field names at the first line
--jsonFormat= the extended JSON format to output, either canonical or relaxed
(defaults to ' relaxed') (default: relaxed)
querying options:
-q, --query= query filter, as a JSON string, e.g., ' {x:{$gt:1}}'
--queryFile= path to a file containing a query filter (JSON)
-k, --slaveOk allow secondary reads if available (default true) (default: false)
--readPreference=| specify either a preference mode (e.g. ' nearest') or a preference
json object (e.g. '{mode: "nearest", tagSets: [{a: "b"}],
maxStalenessSeconds: 123}')
--forceTableScan force a table scan (do not use $snapshot or hint _id). Deprecated
since this is default behavior on WiredTiger
--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}'
--assertExists if specified, export fails if the collection does not exist
(default: false)
数据集合说明
在名为football
的库创建名称为players
的集合,其所有数据如下:
{"_id":ObjectId("5fa5a78ed40acf7364334ceb"),"age":"27","birthday":ISODate("1993-03-03T00:00:00.000Z"),"habit_foot":"右脚","height":"190","league":"英超","name":"吕迪格","national_team":"德国","number":"2","position":"后卫","team_name":"切尔西","weight":"85"}
{"_id":ObjectId("5fa5a78ed40acf7364334cec"),"age":"20","birthday":ISODate("1999-12-08T00:00:00.000Z"),"habit_foot":"右脚","height":"182","league":"英超","name":"里斯-詹姆斯","national_team":"英格兰","number":"24","position":"后卫","team_name":"切尔西","weight":"87"}
{"_id":ObjectId("5fa5a78ed40acf7364334ced"),"age":"23","birthday":ISODate("1996-12-21T00:00:00.000Z"),"habit_foot":"左脚","height":"178","league":"英超","name":"奇尔维尔","national_team":"英格兰","number":"21","position":"后卫","team_name":"切尔西","weight":"77"}
{"_id":ObjectId("5fa5a78ed40acf7364334cee"),"age":"34","birthday":ISODate("1986-09-30T00:00:00.000Z"),"habit_foot":"左脚","height":"193","league":"英超","name":"吉鲁","national_team":"法国","number":"18","position":"前锋","team_name":"切尔西","weight":"92"}
{"_id":ObjectId("5fa5a78ed40acf7364334cef"),"age":"26","birthday":ISODate("1994-10-27T00:00:00.000Z"),"habit_foot":"右脚","height":"190","league":"英超","name":"祖马","national_team":"法国","number":"15","position":"后卫","team_name":"切尔西","weight":"95"}
{"_id":ObjectId("5fa5a78ed40acf7364334cf0"),"age":"29","birthday":ISODate("1991-03-29T00:00:00.000Z"),"habit_foot":"右脚","height":"168","league":"英超","name":"坎特","national_team":"法国","number":"7","position":"中场","team_name":"切尔西","weight":"71"}
{"_id":ObjectId("5fa5a78ed40acf7364334cf1"),"age":"30","birthday":ISODate("1990-03-05T00:00:00.000Z"),"habit_foot":"右脚","height":"177","league":"英超","name":"德林克沃特","national_team":"","number":"-","position":"中场","team_name":"切尔西","weight":"70"}
{"_id":ObjectId("5fa5a78ed40acf7364334cf2"),"age":"26","birthday":ISODate("1994-07-02T00:00:00.000Z"),"habit_foot":"左脚","height":"179","league":"英超","name":"巴巴","national_team":"加纳","number":"-","position":"后卫","team_name":"切尔西","weight":"70"}
{"_id":ObjectId("5fa5a78ed40acf7364334cf3"),"age":"31","birthday":ISODate("1989-08-28T00:00:00.000Z"),"habit_foot":"右脚","height":"178","league":"英超","name":"阿斯皮利奎塔","national_team":"西班牙","number":"28","position":"后卫","team_name":"切尔西","weight":"77"}
{"_id":ObjectId("5fa5a78ed40acf7364334cf4"),"age":"29","birthday":ISODate("1990-12-28T00:00:00.000Z"),"habit_foot":"左脚","height":"188","league":"英超","name":"阿隆索","national_team":"西班牙","number":"3","position":"后卫","team_name":"切尔西","weight":"85"}
{"_id":ObjectId("5fa5a78ed40acf7364334cf5"),"age":"24","birthday":ISODate("1996-04-10T00:00:00.000Z"),"habit_foot":"右脚","height":"188","league":"英超","name":"克里斯滕森","national_team":"丹麦","number":"4","position":"后卫","team_name":"切尔西","weight":"82"}
{"_id":ObjectId("5fa5a78ed40acf7364334cf6"),"age":"39","birthday":ISODate("1981-09-28T00:00:00.000Z"),"habit_foot":"右脚","height":"186","league":"英超","name":"卡巴列罗","national_team":"阿根廷","number":"13","position":"守门员","team_name":"切尔西","weight":"83"}
{"_id":ObjectId("5fa5a78ed40acf7364334cf7"),"age":"26","birthday":ISODate("1994-05-06T00:00:00.000Z"),"habit_foot":"右脚","height":"177","league":"英超","name":"科瓦西奇","national_team":"克罗地亚","number":"17","position":"中场","team_name":"切尔西","weight":"78"}
{"_id":ObjectId("5fa5a78ed40acf7364334cf8"),"age":"22","birthday":ISODate("1997-12-19T00:00:00.000Z"),"habit_foot":"右脚","height":"184","league":"英超","name":"托莫里","national_team":"英格兰","number":"14","position":"后卫","team_name":"切尔西","weight":"75"}
{"_id":ObjectId("5fa5a78ed40acf7364334cf9"),"age":"22","birthday":ISODate("1998-09-18T00:00:00.000Z"),"habit_foot":"右脚","height":"172","league":"英超","name":"普利西奇","national_team":"美国","number":"10","position":"前锋","team_name":"切尔西","weight":"63"}
{"_id":ObjectId("5fa5a78ed40acf7364334cfa"),"age":"36","birthday":ISODate("1984-09-22T00:00:00.000Z"),"habit_foot":"右脚","height":"183","league":"英超","name":"蒂亚戈-席尔瓦","national_team":"巴西","number":"6","position":"后卫","team_name":"切尔西","weight":"79"}
{"_id":ObjectId("5fa5a78ed40acf7364334cfb"),"age":"21","birthday":ISODate("1999-06-11T00:00:00.000Z"),"habit_foot":"左脚","height":"189","league":"英超","name":"哈弗茨","national_team":"德国","number":"29","position":"中场","team_name":"切尔西","weight":"77"}
{"_id":ObjectId("5fa5a78ed40acf7364334cfc"),"age":"26","birthday":ISODate("1994-10-03T00:00:00.000Z"),"habit_foot":"右脚","height":"186","league":"英超","name":"凯帕","national_team":"西班牙","number":"1","position":"守门员","team_name":"切尔西","weight":"88"}
{"_id":ObjectId("5fa5a78ed40acf7364334cfd"),"age":"28","birthday":ISODate("1991-12-20T00:00:00.000Z"),"habit_foot":"右脚","height":"180","league":"英超","name":"若日尼奥","national_team":"意大利","number":"5","position":"中场","team_name":"切尔西","weight":"67"}
{"_id":ObjectId("5fa5a78ed40acf7364334cfe"),"age":"26","birthday":ISODate("1994-08-03T00:00:00.000Z"),"habit_foot":"左脚","height":"181","league":"英超","name":"埃默森","national_team":"意大利","number":"33","position":"后卫","team_name":"切尔西","weight":"79"}
{"_id":ObjectId("5fa5a78ed40acf7364334cff"),"age":"24","birthday":ISODate("1996-03-06T00:00:00.000Z"),"habit_foot":"右脚","height":"181","league":"英超","name":"维尔纳","national_team":"德国","number":"11","position":"前锋","team_name":"切尔西","weight":"75"}
{"_id":ObjectId("5fa5a78ed40acf7364334d00"),"age":"28","birthday":ISODate("1992-03-01T00:00:00.000Z"),"habit_foot":"右脚","height":"196","league":"英超","name":"E.门迪","national_team":"塞内加尔","number":"16","position":"守门员","team_name":"切尔西","weight":"86"}
{"_id":ObjectId("5fa5a78ed40acf7364334d01"),"age":"21","birthday":ISODate("1999-01-10T00:00:00.000Z"),"habit_foot":"右脚","height":"178","league":"英超","name":"芒特","national_team":"英格兰","number":"19","position":"中场","team_name":"切尔西","weight":"65"}
{"_id":ObjectId("5fa5a78ed40acf7364334d02"),"age":"19","birthday":ISODate("2001-06-11T00:00:00.000Z"),"habit_foot":"右脚","height":"170","league":"英超","name":"吉尔莫","national_team":"","number":"23","position":"中场","team_name":"切尔西","weight":"65"}
{"_id":ObjectId("5fa5a78ed40acf7364334d03"),"age":"19","birthday":ISODate("2000-11-07T00:00:00.000Z"),"habit_foot":"右脚","height":"177","league":"英超","name":"奥多伊","national_team":"英格兰","number":"20","position":"前锋","team_name":"切尔西","weight":"74"}
{"_id":ObjectId("5fa5a78ed40acf7364334d04"),"age":"23","birthday":ISODate("1997-10-02T00:00:00.000Z"),"habit_foot":"右脚","height":"191","league":"英超","name":"亚伯拉罕","national_team":"英格兰","number":"9","position":"前锋","team_name":"切尔西","weight":"81"}
{"_id":ObjectId("5fa5a78ed40acf7364334d05"),"age":"24","birthday":ISODate("1996-10-15T00:00:00.000Z"),"habit_foot":"右脚","height":"173","league":"英超","name":"穆松达","national_team":"","number":"-","position":"中场","team_name":"切尔西","weight":"0"}
{"_id":ObjectId("5fa5a78ed40acf7364334d06"),"age":"27","birthday":ISODate("1993-03-19T00:00:00.000Z"),"habit_foot":"左脚","height":"181","league":"英超","name":"齐耶赫","national_team":"摩洛哥","number":"22","position":"前锋","team_name":"切尔西","weight":"70"}
通过mongoexport命令导出数据
现需导出生日位于1990-01-01
到2000-01-01
之间的球员,则执行如下命令:
mongoexport -h 127.0.0.1:27017 -d football -c players --query '{"$and":[{"birthday": {"$gte":{"$date":"1990-01-01T00:00:00Z"}}},{"birthday": {"$lte":{"$date":"2000-01-01T00:00:00Z"}}}]}' --out=
# 此处--out不设置参数, 则为标准输出
上述条件中,日期字符串须符合iso-8601的日期格式。
导出结果如下:
{"_id":{"$oid":"5fa5a78ed40acf7364334ceb"},"age":"27","birthday":{"$date":"1993-03-03T00:00:00Z"},"habit_foot":"右脚","height":"190","league":"英超","name":"吕迪格","national_team":"德国","number":"2","position":"后卫","team_name":"切尔西","weight":"85"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cec"},"age":"20","birthday":{"$date":"1999-12-08T00:00:00Z"},"habit_foot":"右脚","height":"182","league":"英超","name":"里斯-詹姆斯","national_team":"英格兰","number":"24","position":"后卫","team_name":"切尔西","weight":"87"}
{"_id":{"$oid":"5fa5a78ed40acf7364334ced"},"age":"23","birthday":{"$date":"1996-12-21T00:00:00Z"},"habit_foot":"左脚","height":"178","league":"英超","name":"奇尔维尔","national_team":"英格兰","number":"21","position":"后卫","team_name":"切尔西","weight":"77"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cef"},"age":"26","birthday":{"$date":"1994-10-27T00:00:00Z"},"habit_foot":"右脚","height":"190","league":"英超","name":"祖马","national_team":"法国","number":"15","position":"后卫","team_name":"切尔西","weight":"95"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cf0"},"age":"29","birthday":{"$date":"1991-03-29T00:00:00Z"},"habit_foot":"右脚","height":"168","league":"英超","name":"坎特","national_team":"法国","number":"7","position":"中场","team_name":"切尔西","weight":"71"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cf1"},"age":"30","birthday":{"$date":"1990-03-05T00:00:00Z"},"habit_foot":"右脚","height":"177","league":"英超","name":"德林克沃特","national_team":"","number":"-","position":"中场","team_name":"切尔西","weight":"70"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cf2"},"age":"26","birthday":{"$date":"1994-07-02T00:00:00Z"},"habit_foot":"左脚","height":"179","league":"英超","name":"巴巴","national_team":"加纳","number":"-","position":"后卫","team_name":"切尔西","weight":"70"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cf4"},"age":"29","birthday":{"$date":"1990-12-28T00:00:00Z"},"habit_foot":"左脚","height":"188","league":"英超","name":"阿隆索","national_team":"西班牙","number":"3","position":"后卫","team_name":"切尔西","weight":"85"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cf5"},"age":"24","birthday":{"$date":"1996-04-10T00:00:00Z"},"habit_foot":"右脚","height":"188","league":"英超","name":"克里斯滕森","national_team":"丹麦","number":"4","position":"后卫","team_name":"切尔西","weight":"82"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cf7"},"age":"26","birthday":{"$date":"1994-05-06T00:00:00Z"},"habit_foot":"右脚","height":"177","league":"英超","name":"科瓦西奇","national_team":"克罗地亚","number":"17","position":"中场","team_name":"切尔西","weight":"78"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cf8"},"age":"22","birthday":{"$date":"1997-12-19T00:00:00Z"},"habit_foot":"右脚","height":"184","league":"英超","name":"托莫里","national_team":"英格兰","number":"14","position":"后卫","team_name":"切尔西","weight":"75"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cf9"},"age":"22","birthday":{"$date":"1998-09-18T00:00:00Z"},"habit_foot":"右脚","height":"172","league":"英超","name":"普利西奇","national_team":"美国","number":"10","position":"前锋","team_name":"切尔西","weight":"63"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cfb"},"age":"21","birthday":{"$date":"1999-06-11T00:00:00Z"},"habit_foot":"左脚","height":"189","league":"英超","name":"哈弗茨","national_team":"德国","number":"29","position":"中场","team_name":"切尔西","weight":"77"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cfc"},"age":"26","birthday":{"$date":"1994-10-03T00:00:00Z"},"habit_foot":"右脚","height":"186","league":"英超","name":"凯帕","national_team":"西班牙","number":"1","position":"守门员","team_name":"切尔西","weight":"88"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cfd"},"age":"28","birthday":{"$date":"1991-12-20T00:00:00Z"},"habit_foot":"右脚","height":"180","league":"英超","name":"若日尼奥","national_team":"意大利","number":"5","position":"中场","team_name":"切尔西","weight":"67"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cfe"},"age":"26","birthday":{"$date":"1994-08-03T00:00:00Z"},"habit_foot":"左脚","height":"181","league":"英超","name":"埃默森","national_team":"意大利","number":"33","position":"后卫","team_name":"切尔西","weight":"79"}
{"_id":{"$oid":"5fa5a78ed40acf7364334cff"},"age":"24","birthday":{"$date":"1996-03-06T00:00:00Z"},"habit_foot":"右脚","height":"181","league":"英超","name":"维尔纳","national_team":"德国","number":"11","position":"前锋","team_name":"切尔西","weight":"75"}
{"_id":{"$oid":"5fa5a78ed40acf7364334d00"},"age":"28","birthday":{"$date":"1992-03-01T00:00:00Z"},"habit_foot":"右脚","height":"196","league":"英超","name":"E.门迪","national_team":"塞内加尔","number":"16","position":"守门员","team_name":"切尔西","weight":"86"}
{"_id":{"$oid":"5fa5a78ed40acf7364334d01"},"age":"21","birthday":{"$date":"1999-01-10T00:00:00Z"},"habit_foot":"右脚","height":"178","league":"英超","name":"芒特","national_team":"英格兰","number":"19","position":"中场","team_name":"切尔西","weight":"65"}
{"_id":{"$oid":"5fa5a78ed40acf7364334d04"},"age":"23","birthday":{"$date":"1997-10-02T00:00:00Z"},"habit_foot":"右脚","height":"191","league":"英超","name":"亚伯拉罕","national_team":"英格兰","number":"9","position":"前锋","team_name":"切尔西","weight":"81"}
{"_id":{"$oid":"5fa5a78ed40acf7364334d05"},"age":"24","birthday":{"$date":"1996-10-15T00:00:00Z"},"habit_foot":"右脚","height":"173","league":"英超","name":"穆松达","national_team":"","number":"-","position":"中场","team_name":"切尔西","weight":"0"}
{"_id":{"$oid":"5fa5a78ed40acf7364334d06"},"age":"27","birthday":{"$date":"1993-03-19T00:00:00Z"},"habit_foot":"左脚","height":"181","league":"英超","name":"齐耶赫","national_team":"摩洛哥","number":"22","position":"前锋","team_name":"切尔西","weight":"70"}
2020-11-06T15:17:31.843-0500 exported 22 records
官方文档中的示例请参见:**mongoexport
选项**中对于--query
参数的说明。
此外,时间范围的查询还可通过时间的毫秒值来进行,例:
mongoexport -h 127.0.0.1:27017 -d football -c players --query '{"$and":[{"birthday": {"$gte":{"$date":{"$numberLong":"631123200000"}}}},{"birthday": {"$lte":{"$date":{"$numberLong":"946656000000"}}}}]}' --out=
关于该部分时间类型的说明参见:[extented-json-date]^2
以上。