Mongodb获取集合的字段以及数据类型信息
感觉很LOW的一个数据仓工具seatunel,竟然不能自动读取mongodb的表结构信息,需要手工创建。
然鹅,本人对mongodb也是新手,很多操作也不知所措,作为一个DBA,始终还是被研发的甩锅,没得办法只能想办法帮忙把集合的字段类型导出来给他。
网上找了很多方法都不行,坑太多了。
原本想使用mongodump工具导出,但是由于版本限制,mongodb4.4版本才支持schemaonly参数,本人服务器的版本是4.2
在此,为了方便需要的小伙伴,就此贴出无坑版操作代码,希望有所帮助:
winrs:PRIMARY> use testdb;
switched to db testdb
winrs:PRIMARY> var collection = db.dbaList;
winrs:PRIMARY> var fields = {};
winrs:PRIMARY> collection.find().forEach(function(doc) {
... for (var key in doc) {
... if (!fields[key]) {
... fields[key] = typeof doc[key];
... }
... }
... });
winrs:PRIMARY> printjson(fields);
{
"_id" : "number",
"n" : "string",
"brand" : "string",
"stand" : "string",
"group" : "string",
"area" : "string",
"dept" : "string",
"ip" : "string",
"port" : "number",
"sta" : "number",
"c_sta" : "number",
"e_flag" : "number",
"u_t" : "string",
"w_t" : "string",
"sta_t" : "string",
"c_sta_t" : "string",
"stop_t" : "string",
"power" : "number",
"v" : "number",
"s" : "number",
"h" : "number",
"b_a" : "number",
"w_a" : "number",
"h_a" : "number",
"l" : "number",
"e_c" : "object",
"m_c" : "object",
"o_c" : "object",
"timer" : "number",
"count" : "number",
"e_send" : "number",
"m_sta" : "number",
"s_sta" : "number",
"sta_his_id" : "string",
"alert_his_id" : "object",
"c_sta_his_id" : "string",
"alert_time" : "object",
"ensure" : "number",
"update_time" : "string",
"fmis_id" : "string",
"remarks" : "string",
"ensure_time" : "string",
"user_id" : "string"
}