表单生成器(Form Builder)之伪造表单数据mongodb篇
这篇文章终于回到了正轨:为mongodb伪造数据。之前的随机数、随机车牌照、随机时间还有这篇笔记中的获取指定长度的中文字符串,都是为这篇笔记做准备。看一下我们的准备(基础代码)
// 1、获取指定范围随机数-包括最大值和最小值 var getRangeRandomNumber = function(num1,num2){ num1 = Number.isInteger(num1) ? num1: 0; num2 = Number.isInteger(num2) ? num2: 0; var minNum=Math.min(num1,num2),maxNum=Math.max(num1,num2); return Math.round(Math.random() * (maxNum - minNum)) + minNum; }; // 2、格式化日期时间-参考:https://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html var dateExtendFormat = function(date, format) { var o = { "M+": date.getMonth() + 1, "d+": date.getDate(), "H+": date.getHours(), "m+": date.getMinutes(), "s+": date.getSeconds(), "q+": Math.floor((date.getMonth() + 3) / 3), "S": date.getMilliseconds() } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; }; // 3、获取指定范围随机时间,依赖方法 getRangeRandomNumber、dateExtendFormat var getRangeRandomDate=function(date1,date2,format){ var date1ValueOf=new Date(date1).valueOf(),date2ValueOf=new Date(date2).valueOf(); if(isNaN(date1ValueOf)&&isNaN(date2ValueOf)){ date1ValueOf=0; date2ValueOf=new Date().valueOf(); } else{ if(isNaN(date1ValueOf))date1ValueOf=0; if(isNaN(date2ValueOf))date2ValueOf=0; } var retDate=new Date(getRangeRandomNumber(Math.abs(date1ValueOf-date2ValueOf)) + Math.min(date1ValueOf,date2ValueOf)); if(format){ retDate=dateExtendFormat(retDate,format); } return retDate; }; // 4、获取随机车牌照 var getRandomLicensePlate=(function f(excludeArr){ if(!Array.isArray(excludeArr))excludeArr=[]; // 生成一个随机车联牌照 var strProvinceShorter="京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川黔滇藏陕甘青宁新港澳台"; var strNumberLetter="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var tempRetLicensePlate=strProvinceShorter[getRangeRandomNumber(strProvinceShorter.length-1)]; for(var i=0;i<6;i++){ tempRetLicensePlate+=strNumberLetter[getRangeRandomNumber(strNumberLetter.length-1)]; } // 判断这个车联牌照是否存在 if(excludeArr.indexOf(tempRetLicensePlate)>=0){ tempRetLicensePlate = f(excludeArr); } else { excludeArr.push(tempRetLicensePlate); } return tempRetLicensePlate; }); // 5.0、获取一个随机汉字 var getChineseCharacter=function(){ return String.fromCharCode(getRangeRandomNumber(parseInt("4E00",16),parseInt("9FA5",16))); }; // 5.1、获取指定长度的中文字符串 var getCustomChineseCharacters=function(len){ len=isNaN(Number(len))?1:Math.abs(Math.ceil(Number(len))); var retStr=""; for(var i=0;i<len;i++){ retStr+=getChineseCharacter(); } return retStr; };
现在一切都准备好了,那我们就说说正事儿。为了让这些数据有点意义,我想了三个表单。分别是车辆信息表、车辆耗损表以及车辆营收表,下面看一下三张表的结构
车辆信息表(FormId: 507048044944691000, FormVersion: 507048044944691001) |
||||
唯一标识 |
中文描述 |
控件类型 |
是否必填 |
表单项的其他配置(在表单设计时配置,文本框长度、时间格式等) |
1572493551001 |
车辆品牌名称 |
单行文本框 |
是 |
|
1572493551002 |
车辆品牌型号 |
单行文本框 |
是 |
|
1572493551003 |
车辆牌照 |
单行文本框 |
是 |
|
1572493551004 |
车辆购买日期 |
日期时间 |
否 |
|
1572493551005 |
车辆购买价格 |
数值文本框 |
否 |
|
1572493551006 |
备注 |
多行文本框 |
否 |
车辆信息表(FormId: 507048044944691000, FormVersion: 507048044944691002) |
||||
唯一标识 |
中文描述 |
控件类型 |
是否必填 |
表单项的其他配置(在表单设计时配置,文本框长度、时间格式等) |
1572493551001 |
车辆品牌名称 |
单行文本框 |
是 |
|
1572493551002 |
车辆品牌型号 |
单行文本框 |
是 |
|
1572493551003 |
车辆牌照 |
单行文本框 |
是 |
|
1572493551004 |
车辆购买日期 |
时间控件 |
否 |
|
1572493551005 |
车辆购买价格 |
数值文本框 |
否 |
|
1572493551006 |
备注 |
多行文本框 |
否 |
|
……业务需求,删除或者新增一个或者多个字段,生成一个新的版本号 |
车辆耗损表(FormId: 507048044944692000, FormVersion: 507048044944692001) |
||||
唯一标识 |
中文描述 |
控件类型 |
是否必填 |
表单项的其他配置(在表单设计时配置,文本框长度、时间格式等) |
1572493552001 |
所属车辆 |
下拉搜索控件 |
是 |
|
1572493552002 |
耗损人员 |
选择人员控件 |
是 |
|
1572493552003 |
耗损时间 |
时间控件 |
是 |
|
1572493552004 |
耗损类别 |
单行文本框 |
是 |
|
1572493552005 |
耗损金额 |
数值文本框 |
是 |
|
1572493552006 |
备注 |
多行文本框 |
否 |
车辆营收表(FormId: 507048044944693000, FormVersion: 507048044944693001) |
||||
唯一标识 |
中文描述 |
控件类型 |
是否必填 |
表单项的其他配置(在表单设计时配置,文本框长度、时间格式等) |
1572493553001 |
所属车辆 |
下拉搜索控件 |
是 |
|
1572493553002 |
营收人员 |
选择人员控件 |
是 |
|
1572493553003 |
营收时间 |
时间控件 |
是 |
|
1572493553004 |
营收类别 |
单行文本框 |
是 |
|
1572493553005 |
营收金额 |
数值文本框 |
是 |
|
1572493553006 |
里程 |
数值文本框 |
否 |
|
1572493553007 |
备注 |
多行文本框 |
否 |
现在我们就按照表结构伪造数据,下面是全部的代码(可以在Mongodb环境下执行)
// 1、获取指定范围随机数-包括最大值和最小值 var getRangeRandomNumber = function (num1, num2) { num1 = Number.isInteger(num1) ? num1 : 0; num2 = Number.isInteger(num2) ? num2 : 0; var minNum = Math.min(num1, num2), maxNum = Math.max(num1, num2); return Math.round(Math.random() * (maxNum - minNum)) + minNum; }; // 2、格式化日期时间-参考:https://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html var dateExtendFormat = function (date, format) { var o = { "M+": date.getMonth() + 1, "d+": date.getDate(), "H+": date.getHours(), "m+": date.getMinutes(), "s+": date.getSeconds(), "q+": Math.floor((date.getMonth() + 3) / 3), "S": date.getMilliseconds() } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; }; // 3、获取指定范围随机时间,依赖方法 getRangeRandomNumber、dateExtendFormat var getRangeRandomDate = function (date1, date2, format) { var date1ValueOf = new Date(date1).valueOf(), date2ValueOf = new Date(date2).valueOf(); if (isNaN(date1ValueOf) && isNaN(date2ValueOf)) { date1ValueOf = 0; date2ValueOf = new Date().valueOf(); } else { if (isNaN(date1ValueOf)) date1ValueOf = 0; if (isNaN(date2ValueOf)) date2ValueOf = 0; } var retDate = new Date(getRangeRandomNumber(Math.abs(date1ValueOf - date2ValueOf)) + Math.min(date1ValueOf, date2ValueOf)); if (format) { retDate = dateExtendFormat(retDate, format); } return retDate; }; // 4、获取随机车牌照 var getRandomLicensePlate = (function f(excludeArr) { if (!Array.isArray(excludeArr)) excludeArr = []; // 生成一个随机车联牌照 var strProvinceShorter = "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川黔滇藏陕甘青宁新港澳台"; var strNumberLetter = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var tempRetLicensePlate = strProvinceShorter[getRangeRandomNumber(strProvinceShorter.length - 1)]; for (var i = 0; i < 6; i++) { tempRetLicensePlate += strNumberLetter[getRangeRandomNumber(strNumberLetter.length - 1)]; } // 判断这个车联牌照是否存在 if (excludeArr.indexOf(tempRetLicensePlate) >= 0) { tempRetLicensePlate = f(excludeArr); } else { excludeArr.push(tempRetLicensePlate); } return tempRetLicensePlate; }); // 5.0、获取一个随机汉字 var getChineseCharacter = function () { return String.fromCharCode(getRangeRandomNumber(parseInt("4E00", 16), parseInt("9FA5", 16))); }; // 5.1、获取指定长度的中文字符串 var getCustomChineseCharacters = function (len) { len = isNaN(Number(len)) ? 1 : Math.abs(Math.ceil(Number(len))); var retStr = ""; for (var i = 0; i < len; i++) { retStr += getChineseCharacter(); } return retStr; }; // 插入数据方法 var handleBatchInsertData = function (initConfig) { if (initConfig.formId && initConfig.formVersion && Array.isArray(initConfig.source) && initConfig.source.length) { var tempTrs = [], tempCreateDate = ISODate(), tempFormInstaceDataCounter = db.getCollection('FormInstace').find({}).count(); initConfig.source.forEach((formItemObj) => { var tempCreateUserIdIndex = Math.floor(Math.random() * GV_CreateUserIds.length); var tempTr = { _id: (++tempFormInstaceDataCounter).toString(), FormItems: [], ExtendData: {}, CreateUserId: GV_CreateUserIds[tempCreateUserIdIndex], CreateUserName: GV_CreateUserIds[tempCreateUserIdIndex], CreateDate: tempCreateDate, LastModifyDate: tempCreateDate, FormId: initConfig.formId, FormVersion: initConfig.formVersion }; for (var i in formItemObj) { if (formItemObj.hasOwnProperty(i)) { tempTr.FormItems.push({ key: i, value: formItemObj[i] }); } } tempTrs.push(tempTr); }); try { db.FormInstace.insertMany(tempTrs); } catch (e) { print(e); } } }; // 创建用户集合 var GV_CreateUserIds = ["user10000", "user10001", "user10002", "user10003", "user10004", "user10005", "user10006", "user10007", "user10008", "user10009"]; // 车辆信息表插入数据配置 var GV_CarInfoInitConfig = { formId: "507048044944691000", formVersion: "507048044944691001", // carModelMin~carModelMax 这两个数之间生成一个随机数,作为生成每个车型的数量(数据库条数) carModelMin: 10, carModelMax: 100, carTypes: [{ brandName: "红旗", models: [{ name: "HS7", min: 349800, max: 459800 }, { name: "HS5", min: 183800, max: 249800 }, { name: "H7", min: 252800, max: 317800 }, { name: "H5", min: 146800, max: 190800 }, { name: "HE-HS3", min: 225800, max: 317800 } ] }, { brandName: "比亚迪", models: [{ name: "元", min: 65900, max: 99900 }, { name: "宋", min: 79800, max: 119800 }, { name: "宋Pro", min: 89800, max: 119800 }, { name: "唐", min: 129900, max: 169900 }, { name: "比亚迪F3", min: 43900, max: 65900 }, { name: "速锐", min: 59900, max: 69900 }, { name: "秦Pro", min: 79800, max: 115900 }, { name: "宋MAX", min: 79900, max: 129900 }, { name: "元EV", min: 89900, max: 139900 }, { name: "比亚迪S2", min: 89800, max: 109800 }, { name: "宋DM", min: 176900, max: 206900 }, { name: "宋Pro DM", min: 169800, max: 259900 }, { name: "宋EV", min: 189900, max: 219900 }, { name: "宋Pro EV", min: 179800, max: 259900 }, { name: "唐DM", min: 229900, max: 329900 }, { name: "唐EV", min: 259900, max: 409900 }, { name: "比亚迪e1", min: 59900, max: 79900 }, { name: "比亚迪e2", min: 89800, max: 119800 }, { name: "比亚迪e3", min: 103800, max: 139800 }, { name: "秦DM", min: 132900, max: 209900 }, { name: "秦Pro DM", min: 136900, max: 206900 }, { name: "秦EV", min: 149900, max: 169900 }, { name: "秦Pro EV", min: 149900, max: 299900 }, { name: "宋MAX DM", min: 149900, max: 196900 }, { name: "宋MAX EV", min: 179800, max: 199800 } ] }, { brandName: "奇瑞", models: [{ name: "瑞虎3", min: 59900, max: 79900 }, { name: "瑞虎3x", min: 49900, max: 62900 }, { name: "瑞虎5x", min: 59900, max: 89900 }, { name: "瑞虎7", min: 85900, max: 150900 }, { name: "瑞虎8", min: 88800, max: 155900 }, { name: "艾瑞泽5", min: 49900, max: 84900 }, { name: "艾瑞泽GX", min: 74900, max: 113900 }, { name: "瑞虎3xe", min: 175800, max: 189800 }, { name: "瑞虎e", min: 109900, max: 143900 }, { name: "奇瑞eQ1", min: 59800, max: 75800 }, { name: "艾瑞泽e", min: 126800, max: 143800 }, { name: "艾瑞泽5e", min: 192300, max: 212300 } ] }, { brandName: "吉利", models: [{ name: "远景X1", min: 39900, max: 57900 }, { name: "远景X3", min: 45900, max: 68900 }, { name: "缤越", min: 78800, max: 129800 }, { name: "远景S1", min: 59900, max: 94900 }, { name: "远景X6", min: 68900, max: 105900 }, { name: "帝豪GS", min: 77800, max: 116800 }, { name: "博越", min: 88800, max: 161800 }, { name: "星越", min: 135800, max: 195800 }, { name: "金刚", min: 47900, max: 65900 }, { name: "远景", min: 47900, max: 73900 }, { name: "帝豪", min: 68900, max: 98800 }, { name: "帝豪GL", min: 78800, max: 121800 }, { name: "缤瑞", min: 75800, max: 110800 }, { name: "博瑞", min: 136800, max: 179800 }, { name: "嘉际", min: 99800, max: 148800 }, { name: "缤越PHEV", min: 149800, max: 169800 }, { name: "帝豪GSe", min: 119800, max: 159800 }, { name: "星越PHEV", min: 188800, max: 216800 }, { name: "帝豪EV", min: 135800, max: 238300 }, { name: "帝豪PHEV", min: 165800, max: 185800 }, { name: "帝豪GL PHEV", min: 152800, max: 164800 }, { name: "博瑞PHEV", min: 176800, max: 209800 }, { name: "嘉际PHEV", min: 169800, max: 192800 } ] }, { brandName: "长安", models: [{ name: "长安CS15", min: 55900, max: 80900 }, { name: "长安CS35", min: 63900, max: 87900 }, { name: "长安CS35 PLUS", min: 69900, max: 109900 }, { name: "长安CS55", min: 82900, max: 133900 }, { name: "长安CS75", min: 79800, max: 174800 }, { name: "长安CS75 PLUS", min: 106900, max: 154900 }, { name: "长安CS85 COUPE", min: 119900, max: 169900 }, { name: "长安CS95", min: 165900, max: 213900 }, { name: "奔奔", min: 40900, max: 56900 }, { name: "悦翔", min: 49900, max: 67900 }, { name: "逸动DT", min: 52900, max: 80900 }, { name: "逸动XT", min: 77900, max: 111900 }, { name: "逸动", min: 69900, max: 103900 }, { name: "睿骋CC", min: 84900, max: 135900 }, { name: "锐程CC", min: 94900, max: 128900 }, { name: "睿骋", min: 120800, max: 150800 }, { name: "凌轩", min: 67900, max: 110900 }, { name: "长安CS15 EV", min: 89800, max: 98800 }, { name: "长安CS75 PHEV", min: 175800, max: 206800 }, { name: "奔奔EV", min: 49800, max: 81800 }, { name: "逸动ET", min: 132900, max: 142900 }, { name: "逸动PHEV", min: 160900, max: 169900 }, { name: "逸动EV", min: 129900, max: 139900 }, ] }, { brandName: "长安欧尚", models: [{ name: "长安欧尚科赛3", min: 59900, max: 76900 }, { name: "长安欧尚科赛5", min: 69900, max: 82900 }, { name: "长安欧尚X70A", min: 49900, max: 859500 }, { name: "长安欧尚CX70", min: 59900, max: 109900 }, { name: "长安欧尚科赛", min: 86800, max: 152800 }, { name: "欧尚S", min: 39900, max: 55900 }, { name: "欧尚长行", min: 68900, max: 82900 }, { name: "长安欧尚A600", min: 49900, max: 84900 }, { name: "长安欧尚A800", min: 62900, max: 100900 }, { name: "长安欧尚科尚", min: 79800, max: 160800 }, { name: "长安之星9", min: 47800, max: 48800 }, { name: "长安星卡", min: 32900, max: 47900 }, { name: "尼欧Ⅱ", min: 88800, max: 100800 }, { name: "欧力威EV", min: 105800, max: 105800 }, { name: "欧诺S EV", min: 175800, max: 175800 }, { name: "欧尚长行EV", min: 169800, max: 169800 }, { name: "欧尚EV", min: 169800, max: 169800 }, { name: "欧尚A600 EV", min: 149800, max: 149800 }, { name: "长安之星9 EV", min: 152800, max: 156000 }, { name: "长安星卡EV", min: 123800, max: 125800 }, ] }, { brandName: "长安轻型车", models: [{ name: "睿行S50", min: 48900, max: 77900 }, { name: "睿行S50T", min: 61900, max: 78900 }, { name: "睿行M60", min: 51900, max: 59900 }, { name: "睿行M70", min: 60500, max: 74000 }, { name: "睿行M80", min: 58500, max: 72000 }, { name: "睿行M90", min: 68500, max: 92500 }, { name: "神骐F30", min: 47600, max: 63800 }, { name: "神骐T10", min: 39900, max: 50900 }, { name: "神骐T20", min: 33900, max: 59700 }, { name: "凯程F70", min: 92800, max: 139800 }, { name: "长安星卡L系列", min: 39900, max: 47900 }, { name: "长安星卡C系列", min: 30900, max: 33600 }, { name: "睿行ES30", min: 66800, max: 69800 }, { name: "睿行EM60", min: 122800, max: 122800 }, { name: "睿行EM80", min: 96800, max: 120000 } ] }, { brandName: "长安跨越", models: [{ name: "长安跨越V3", min: 33500, max: 34300 }, { name: "跨越王X1", min: 39600, max: 51800 }, { name: "跨越王X3", min: 45700, max: 58400 }, { name: "长安跨越王X5", min: 47400, max: 65600 }, { name: "长安新豹2", min: 40700, max: 58600 }, { name: "新豹3", min: 46200, max: 59900 }, { name: "新豹T3", min: 41300, max: 51800 }, { name: "长安新豹MINI", min: 32100, max: 50500 }, { name: "跨越者D5", min: 59300, max: 67700 }, { name: "长安跨越新能源V3 EV", min: 79200, max: 79200 }, { name: "长安跨越新能源V5 EV", min: 93800, max: 93800 } ] }, { brandName: "力帆", models: [{ name: "迈威", min: 56800, max: 73800 }, { name: "力帆X80", min: 109900, max: 149900 }, { name: "力帆820", min: 76800, max: 119800 }, { name: "轩朗", min: 69800, max: 106800 }, { name: "乐途", min: 35800, max: 59800 }, { name: "力帆650EV", min: 168900, max: 175800 }, { name: "力帆820EV", min: 256800, max: 279800 } ] }, { brandName: "长城", models: [{ name: "风骏5", min: 68800, max: 112800 }, { name: "风骏6", min: 86800, max: 117800 }, { name: "风骏7", min: 86800, max: 138800 }, { name: "炮", min: 97800, max: 159800 }, { name: "长城C30 EV", min: 150000, max: 154000 } ] }, { brandName: "哈弗", models: [{ name: "哈弗H2", min: 74900, max: 95900 }, { name: "哈弗H2s", min: 70000, max: 85000 }, { name: "哈弗H4", min: 73900, max: 115000 }, { name: "哈弗F5", min: 100000, max: 130000 }, { name: "哈弗H5", min: 107800, max: 136800 }, { name: "哈弗M6", min: 66000, max: 82000 }, { name: "哈弗H6", min: 102000, max: 136000 }, { name: "哈弗H6 Coupe", min: 79900, max: 119000 }, { name: "哈弗H7", min: 142000, max: 180000 }, { name: "哈弗F7", min: 109000, max: 153700 }, { name: "哈弗F7x", min: 119900, max: 154900 }, { name: "哈弗H9", min: 209800, max: 272800 }, ] }, { brandName: "江铃", models: [{ name: "宝典", min: 78800, max: 105300 }, { name: "凯锐800", min: 120800, max: 129100 }, { name: "凯运强劲版", min: 99500, max: 117400 }, { name: "凯运升级版", min: 94800, max: 106800 }, { name: "顺达宽体", min: 90800, max: 100000 }, { name: "顺达窄体", min: 84000, max: 89000 }, { name: "特顺", min: 101300, max: 142700 }, { name: "域虎3", min: 89800, max: 122200 }, { name: "域虎5", min: 96800, max: 136300 }, { name: "域虎7", min: 119800, max: 176300 }, { name: "江铃E100B", min: 73800, max: 73800 }, { name: "江铃E160", min: 95800, max: 98800 }, { name: "江铃E200L", min: 87800, max: 90800 }, { name: "江铃E200N", min: 90800, max: 93800 }, { name: "易至EV3", min: 66800, max: 83800 }, { name: "易至EX5", min: 89800, max: 122800 }, { name: "特顺EV", min: 206000, max: 206000 }, { name: "骐铃T5", min: 65800, max: 86800 }, { name: "骐铃T7", min: 72800, max: 113800 }, { name: "骐铃T100", min: 53800, max: 71800 }, { name: "D-MAX房车", min: 283000, max: 396000 }, { name: "考斯特房车", min: 318000, max: 380000 }, { name: "旅居房车", min: 536000, max: 596000 }, { name: "罗莎房车租赁款", min: 268000, max: 288000 }, { name: "骐铃T7皮卡房车", min: 258000, max: 299800 }, { name: "全顺商旅房车", min: 468000, max: 518000 }, { name: "全顺T型房车", min: 408000, max: 448000 }, { name: "商旅房车", min: 438000, max: 488000 }, { name: "商旅房车经典款", min: 388000, max: 428000 }, { name: "途睿欧商务车", min: 298000, max: 398000 } ] }, ], source: [] }; var GV_TempCarLicensePlates = []; GV_CarInfoInitConfig.carTypes.forEach((carType) => { if (Array.isArray(carType.models) && carType.models.length) { carType.models.forEach((model) => { for (var i = 0; i < getRangeRandomNumber(GV_CarInfoInitConfig.carModelMin, GV_CarInfoInitConfig.carModelMax); i++) { // 这里会生成一条车辆信息记录(一辆真正的车,有车牌号) var tempPlate = getRandomLicensePlate(GV_TempCarLicensePlates); GV_CarInfoInitConfig.source.push({ "1572493551001": carType.brandName, "1572493551002": model.name, "1572493551003": tempPlate, "1572493551004": getRangeRandomDate("2010-01-01", "2018-06-06", "yyyy-MM-dd HH:mm:ss"), "1572493551005": getRangeRandomNumber(model.min, model.max), "1572493551006": getCustomChineseCharacters(getRangeRandomNumber(200)) }); } }); } }); // 车辆花费表插入数据配置 var GV_CarWastageInitConfig = { formId: "507048044944692000", formVersion: "507048044944692001", // carWastageMin~carWastageMax 这两个数之间生成一个随机数,作为每辆车的花费记录(数据库条数) carWastageMin: 50, carWastageMax: 100, wastageType: [{ text: "违章", detailed: ["信号灯", "压线", "单双号", "规定方向行驶", "避让特殊车辆", "避让行人", "时间道路禁行"] }, { text: "洗车", detailed: ["全套", "外围"] }, { text: "修车", detailed: ["轮胎", "前玻璃", "后玻璃", "坐垫", "前大灯", "后大灯", "车窗", "发动机", "电瓶", "变速箱"] }, { text: "加油", detailed: ["92", "93", "95", "97"] }, { text: "保险", detailed: ["太平洋", "平安", "人保", "泰康"] }, { text: "肇事", detailed: ["伤人", "护栏", "撞车", "间接事故", "其他"] } ], source: [] }; // 车辆营收表插入数据配置 var GV_CarRevenueInitConfig = { formId: "507048044944693000", formVersion: "507048044944693001", // carRevenueMin~carRevenueMax 这两个数之间生成一个随机数,作为每辆车的营收记录(数据库条数) carRevenueMin: 100, carRevenueMax: 200, revenueTypes: [{ text: "租赁", detailed: ["一天", "一周", "一月", "一年"] }, { text: "载客", detailed: ["线下", "滴滴", "Uber", "快的", "其他"] }, { text: "送货", detailed: ["手机", "笔记本", "照相机", "微波炉", "电磁率", "其他"] }, { text: "其他", detailed: ["婚车", "其他"] } ], source: [] }; // 根据已经生成的车辆信息生成车辆的花费和营收记录 if (GV_CarInfoInitConfig.source.length) { GV_CarInfoInitConfig.source.forEach((carInfo, carInfoIndex) => { for (var i = 0; i < getRangeRandomNumber(GV_CarWastageInitConfig.carWastageMin, GV_CarWastageInitConfig.carWastageMax); i++) { // 这里会生成某一辆车花费记录 var tempUserIdIndexWastageType = getRangeRandomNumber(GV_CreateUserIds.length - 1); var tempWastageTypeIndex = getRangeRandomNumber(GV_CarWastageInitConfig.wastageType.length - 1); var tempWastageTypeDetailIndex = getRangeRandomNumber(GV_CarWastageInitConfig.wastageType[tempWastageTypeIndex].detailed.length - 1); GV_CarWastageInitConfig.source.push({ "1572493552001": { id: (carInfoIndex + 1).toString(), name: carInfo["1572493551003"], value: "" }, "1572493552002": [{ id: GV_CreateUserIds[tempUserIdIndexWastageType], name: GV_CreateUserIds[tempUserIdIndexWastageType], face: "" } ], "1572493552003": getRangeRandomDate(carInfo["1572493551004"], "2019-11-07", "yyyy-MM-dd HH:mm:ss"), "1572493552004": GV_CarWastageInitConfig.wastageType[tempWastageTypeIndex].text, "1572493552005": getRangeRandomNumber(20, 10000), "1572493552006": GV_CarWastageInitConfig.wastageType[tempWastageTypeIndex].detailed[tempWastageTypeDetailIndex] }); } for (var i = 0; i < getRangeRandomNumber(GV_CarRevenueInitConfig.carRevenueMin, GV_CarRevenueInitConfig.carRevenueMax); i++) { // 这里会生成某一辆车营收记录 var tempUserIdIndexRevenue = getRangeRandomNumber(GV_CreateUserIds.length - 1); var tempRevenueIndex = getRangeRandomNumber(GV_CarRevenueInitConfig.revenueTypes.length - 1); var tempRevenueDetailIndex = getRangeRandomNumber(GV_CarRevenueInitConfig.revenueTypes[tempRevenueIndex].detailed.length - 1); GV_CarRevenueInitConfig.source.push({ "1572493553001": { id: (carInfoIndex + 1).toString(), name: carInfo["1572493551003"], value: "" }, "1572493553002": [{ id: GV_CreateUserIds[tempUserIdIndexRevenue], name: GV_CreateUserIds[tempUserIdIndexRevenue], face: "" } ], "1572493553003": getRangeRandomDate(carInfo["1572493551004"], "2019-11-07", "yyyy-MM-dd HH:mm:ss"), "1572493553004": GV_CarRevenueInitConfig.revenueTypes[tempRevenueIndex].text, "1572493553005": getRangeRandomNumber(20, 10000), "1572493553006": getRangeRandomNumber(50, 3000), "1572493553007": GV_CarRevenueInitConfig.revenueTypes[tempRevenueIndex].detailed[tempRevenueDetailIndex] }); } }); } handleBatchInsertData(GV_CarInfoInitConfig); handleBatchInsertData(GV_CarWastageInitConfig); handleBatchInsertData(GV_CarRevenueInitConfig);
看一下数据库中的截图
一共生成了656084条数据,我觉得还是不错了,没白折腾……当然你可以将随机数调的大一些,便可以生成更多的数据,就到这里吧!
【2021-06-20伪造数据代码更新】
添加一些注释;更好控制插入数据的条数
【2021-07-13伪造数据代码更新】
这次更新的改动比较大,经历好几个版本……最初是主键ID是自增的,提前把要插入的数据主备好,此时数据量小(五六十万)的时候还可以,但是如果上百万,电脑的内存就不够用了,还有一个问题就是不能重复插入;再后来就是将ID换成GUID+分页插入,效果也不是很理想;最后的这个版本是以车辆为单位,每次插入车辆信息和对应的车辆花费、车辆收入数据(如果数据超过设置的阈值,分页插入),这次改动之后就可以重复插入数据(执行这个语句)了,如果你要插入的数据量比较大,一定要使用“mongo”执行JS语句,千万别使用Robo 3T、Navicat了,使用mongo Shell会快的多,这里测试前1000万数据用了一个小时,后一千万就比较慢了,此时MongoDB Database Server内存占了三四个G,还有就是感觉磁盘速度跟不上……不说了,看一下这次的代码
/* * 🚗🚗🚗写在最前面🚗🚗🚗 * 进入命令行(cmd/PowerShell),执行下面的命令: * > mongo localhost:27017/ddz001 xxx.js * 说明: * 1、IP地址:端口/数据库 * 2、xxx.js文件的内容就是下面的这些代码 * 参考链接 * 1、mongo Shell中的数据类型-》[Data Types in the mongo Shell — MongoDB Manual](https://docs.mongodb.com/manual/core/shell-types/) * 2、mongo Shell执行JS文件-》[Write Scripts for the mongo Shell — MongoDB Manual](https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/) * 3、mongo Shell执行JS文件-》[Executing a JavaScript File for MongoDB](https://documentation.signavio.com/suite/en-us/Content/workflow-accelerator/adminguide/execute-javascript.htm) * 4、for循环优化(这个最后没有用到)-》[js 优化for循环-Duff装置 - SegmentFault 思否](https://segmentfault.com/a/1190000023524594) */ // 1、获取指定范围随机数-包括最大值和最小值 var getRangeRandomNumber = function (num1, num2) { num1 = Number.isInteger(num1) ? num1 : 0; num2 = Number.isInteger(num2) ? num2 : 0; var minNum = Math.min(num1, num2), maxNum = Math.max(num1, num2); return Math.round(Math.random() * (maxNum - minNum)) + minNum; }; // 2、格式化日期时间-参考:https://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html var dateExtendFormat = function (date, format) { var o = { "M+": date.getMonth() + 1, "d+": date.getDate(), "H+": date.getHours(), "m+": date.getMinutes(), "s+": date.getSeconds(), "q+": Math.floor((date.getMonth() + 3) / 3), "S": date.getMilliseconds() } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; }; // 3、获取指定范围随机时间,依赖方法 getRangeRandomNumber、dateExtendFormat var getRangeRandomDate = function (date1, date2, format) { var date1ValueOf = new Date(date1).valueOf(), date2ValueOf = new Date(date2).valueOf(); if (isNaN(date1ValueOf) && isNaN(date2ValueOf)) { date1ValueOf = 0; date2ValueOf = new Date().valueOf(); } else { if (isNaN(date1ValueOf)) date1ValueOf = 0; if (isNaN(date2ValueOf)) date2ValueOf = 0; } var retDate = new Date(getRangeRandomNumber(Math.abs(date1ValueOf - date2ValueOf)) + Math.min(date1ValueOf, date2ValueOf)); if (format) { retDate = dateExtendFormat(retDate, format); } return retDate; }; // 4、获取随机车牌照 var getRandomLicensePlate = (function f(excludeArr) { if (!Array.isArray(excludeArr)) excludeArr = []; // 生成一个随机车联牌照 var strProvinceShorter = "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川黔滇藏陕甘青宁新港澳台"; var strNumberLetter = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var tempRetLicensePlate = strProvinceShorter[getRangeRandomNumber(strProvinceShorter.length - 1)]; for (var i = 0; i < 6; i++) { tempRetLicensePlate += strNumberLetter[getRangeRandomNumber(strNumberLetter.length - 1)]; } // 判断这个车联牌照是否存在 if (excludeArr.indexOf(tempRetLicensePlate) >= 0) { tempRetLicensePlate = f(excludeArr); } else { excludeArr.push(tempRetLicensePlate); } return tempRetLicensePlate; }); // 5.0、获取一个随机汉字 var getChineseCharacter = function () { return String.fromCharCode(getRangeRandomNumber(parseInt("4E00", 16), parseInt("9FA5", 16))); }; // 5.1、获取指定长度的中文字符串 var getCustomChineseCharacters = function (len) { len = isNaN(Number(len)) ? 1 : Math.abs(Math.ceil(Number(len))); var retStr = ""; for (var i = 0; i < len; i++) { retStr += getChineseCharacter(); } return retStr; }; // 6、获取指定长度的中文字符串 var getGUID = function () { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16).toUpperCase(); }); } //************************************************************************************************************************************************************** // 每页数量(控制单次插入到数据库的数量) var pageSize = 10000; // 创建用户集合 var GV_CreateUserIds = ["user10000", "user10001", "user10002", "user10003", "user10004", "user10005", "user10006", "user10007", "user10008", "user10009"]; // 插入数据方法 var realBatchInsertData = function (trs) { try { db.FormInstace.insertMany(trs); } catch (e) { print(e); } }; var getFormInstanceOtherAttrs = function (formId, formVersion) { var tempCreateUserIdIndex = Math.floor(Math.random() * GV_CreateUserIds.length), tempCreateDate = ISODate(); return { ExtendData: {}, CreateUserId: GV_CreateUserIds[tempCreateUserIdIndex], CreateUserName: GV_CreateUserIds[tempCreateUserIdIndex], CreateDate: tempCreateDate, LastModifyDate: tempCreateDate, FormId: formId, FormVersion: formVersion }; }; // 车辆花费表插入数据配置 var GV_CarWastageInitConfig = { formId: "507048044944692000", formVersion: "507048044944692001", formName: "车辆花费表", // carWastageMin~carWastageMax 这两个数之间生成一个随机数,作为每辆车的花费记录(数据库条数) carWastageMin: 50, carWastageMax: 100, pageIndex: 0, wastageType: [{ text: "违章", detailed: ["信号灯", "压线", "单双号", "规定方向行驶", "避让特殊车辆", "避让行人", "时间道路禁行"] }, { text: "洗车", detailed: ["全套", "外围"] }, { text: "修车", detailed: ["轮胎", "前玻璃", "后玻璃", "坐垫", "前大灯", "后大灯", "车窗", "发动机", "电瓶", "变速箱"] }, { text: "加油", detailed: ["92", "93", "95", "97"] }, { text: "保险", detailed: ["太平洋", "平安", "人保", "泰康"] }, { text: "肇事", detailed: ["伤人", "护栏", "撞车", "间接事故", "其他"] } ] }; // 车辆营收表插入数据配置 var GV_CarRevenueInitConfig = { formId: "507048044944693000", formVersion: "507048044944693001", formName: "车辆营收表", // carRevenueMin~carRevenueMax 这两个数之间生成一个随机数,作为每辆车的营收记录(数据库条数) carRevenueMin: 100, carRevenueMax: 200, pageIndex: 0, revenueTypes: [{ text: "租赁", detailed: ["一天", "一周", "一月", "一年"] }, { text: "载客", detailed: ["线下", "滴滴", "Uber", "快的", "其他"] }, { text: "送货", detailed: ["手机", "笔记本", "照相机", "微波炉", "电磁率", "其他"] }, { text: "其他", detailed: ["婚车", "其他"] } ] }; // 车辆信息表插入数据配置 var GV_CarInfoInitConfig = { formId: "507048044944691000", formVersion: "507048044944691001", formName: "车辆信息表", // carModelMin~carModelMax 这两个数之间生成一个随机数,作为生成每个车型的数量(数据库条数) carModelMin: 10, carModelMax: 100, carTypes: [{ brandName: "红旗", models: [{ name: "HS7", min: 349800, max: 459800 }, { name: "HS5", min: 183800, max: 249800 }, { name: "H7", min: 252800, max: 317800 }, { name: "H5", min: 146800, max: 190800 }, { name: "HE-HS3", min: 225800, max: 317800 } ] }, { brandName: "比亚迪", models: [{ name: "元", min: 65900, max: 99900 }, { name: "宋", min: 79800, max: 119800 }, { name: "宋Pro", min: 89800, max: 119800 }, { name: "唐", min: 129900, max: 169900 }, { name: "比亚迪F3", min: 43900, max: 65900 }, { name: "速锐", min: 59900, max: 69900 }, { name: "秦Pro", min: 79800, max: 115900 }, { name: "宋MAX", min: 79900, max: 129900 }, { name: "元EV", min: 89900, max: 139900 }, { name: "比亚迪S2", min: 89800, max: 109800 }, { name: "宋DM", min: 176900, max: 206900 }, { name: "宋Pro DM", min: 169800, max: 259900 }, { name: "宋EV", min: 189900, max: 219900 }, { name: "宋Pro EV", min: 179800, max: 259900 }, { name: "唐DM", min: 229900, max: 329900 }, { name: "唐EV", min: 259900, max: 409900 }, { name: "比亚迪e1", min: 59900, max: 79900 }, { name: "比亚迪e2", min: 89800, max: 119800 }, { name: "比亚迪e3", min: 103800, max: 139800 }, { name: "秦DM", min: 132900, max: 209900 }, { name: "秦Pro DM", min: 136900, max: 206900 }, { name: "秦EV", min: 149900, max: 169900 }, { name: "秦Pro EV", min: 149900, max: 299900 }, { name: "宋MAX DM", min: 149900, max: 196900 }, { name: "宋MAX EV", min: 179800, max: 199800 } ] }, { brandName: "奇瑞", models: [{ name: "瑞虎3", min: 59900, max: 79900 }, { name: "瑞虎3x", min: 49900, max: 62900 }, { name: "瑞虎5x", min: 59900, max: 89900 }, { name: "瑞虎7", min: 85900, max: 150900 }, { name: "瑞虎8", min: 88800, max: 155900 }, { name: "艾瑞泽5", min: 49900, max: 84900 }, { name: "艾瑞泽GX", min: 74900, max: 113900 }, { name: "瑞虎3xe", min: 175800, max: 189800 }, { name: "瑞虎e", min: 109900, max: 143900 }, { name: "奇瑞eQ1", min: 59800, max: 75800 }, { name: "艾瑞泽e", min: 126800, max: 143800 }, { name: "艾瑞泽5e", min: 192300, max: 212300 } ] }, { brandName: "吉利", models: [{ name: "远景X1", min: 39900, max: 57900 }, { name: "远景X3", min: 45900, max: 68900 }, { name: "缤越", min: 78800, max: 129800 }, { name: "远景S1", min: 59900, max: 94900 }, { name: "远景X6", min: 68900, max: 105900 }, { name: "帝豪GS", min: 77800, max: 116800 }, { name: "博越", min: 88800, max: 161800 }, { name: "星越", min: 135800, max: 195800 }, { name: "金刚", min: 47900, max: 65900 }, { name: "远景", min: 47900, max: 73900 }, { name: "帝豪", min: 68900, max: 98800 }, { name: "帝豪GL", min: 78800, max: 121800 }, { name: "缤瑞", min: 75800, max: 110800 }, { name: "博瑞", min: 136800, max: 179800 }, { name: "嘉际", min: 99800, max: 148800 }, { name: "缤越PHEV", min: 149800, max: 169800 }, { name: "帝豪GSe", min: 119800, max: 159800 }, { name: "星越PHEV", min: 188800, max: 216800 }, { name: "帝豪EV", min: 135800, max: 238300 }, { name: "帝豪PHEV", min: 165800, max: 185800 }, { name: "帝豪GL PHEV", min: 152800, max: 164800 }, { name: "博瑞PHEV", min: 176800, max: 209800 }, { name: "嘉际PHEV", min: 169800, max: 192800 } ] }, { brandName: "长安", models: [{ name: "长安CS15", min: 55900, max: 80900 }, { name: "长安CS35", min: 63900, max: 87900 }, { name: "长安CS35 PLUS", min: 69900, max: 109900 }, { name: "长安CS55", min: 82900, max: 133900 }, { name: "长安CS75", min: 79800, max: 174800 }, { name: "长安CS75 PLUS", min: 106900, max: 154900 }, { name: "长安CS85 COUPE", min: 119900, max: 169900 }, { name: "长安CS95", min: 165900, max: 213900 }, { name: "奔奔", min: 40900, max: 56900 }, { name: "悦翔", min: 49900, max: 67900 }, { name: "逸动DT", min: 52900, max: 80900 }, { name: "逸动XT", min: 77900, max: 111900 }, { name: "逸动", min: 69900, max: 103900 }, { name: "睿骋CC", min: 84900, max: 135900 }, { name: "锐程CC", min: 94900, max: 128900 }, { name: "睿骋", min: 120800, max: 150800 }, { name: "凌轩", min: 67900, max: 110900 }, { name: "长安CS15 EV", min: 89800, max: 98800 }, { name: "长安CS75 PHEV", min: 175800, max: 206800 }, { name: "奔奔EV", min: 49800, max: 81800 }, { name: "逸动ET", min: 132900, max: 142900 }, { name: "逸动PHEV", min: 160900, max: 169900 }, { name: "逸动EV", min: 129900, max: 139900 }, ] }, { brandName: "长安欧尚", models: [{ name: "长安欧尚科赛3", min: 59900, max: 76900 }, { name: "长安欧尚科赛5", min: 69900, max: 82900 }, { name: "长安欧尚X70A", min: 49900, max: 859500 }, { name: "长安欧尚CX70", min: 59900, max: 109900 }, { name: "长安欧尚科赛", min: 86800, max: 152800 }, { name: "欧尚S", min: 39900, max: 55900 }, { name: "欧尚长行", min: 68900, max: 82900 }, { name: "长安欧尚A600", min: 49900, max: 84900 }, { name: "长安欧尚A800", min: 62900, max: 100900 }, { name: "长安欧尚科尚", min: 79800, max: 160800 }, { name: "长安之星9", min: 47800, max: 48800 }, { name: "长安星卡", min: 32900, max: 47900 }, { name: "尼欧Ⅱ", min: 88800, max: 100800 }, { name: "欧力威EV", min: 105800, max: 105800 }, { name: "欧诺S EV", min: 175800, max: 175800 }, { name: "欧尚长行EV", min: 169800, max: 169800 }, { name: "欧尚EV", min: 169800, max: 169800 }, { name: "欧尚A600 EV", min: 149800, max: 149800 }, { name: "长安之星9 EV", min: 152800, max: 156000 }, { name: "长安星卡EV", min: 123800, max: 125800 }, ] }, { brandName: "长安轻型车", models: [{ name: "睿行S50", min: 48900, max: 77900 }, { name: "睿行S50T", min: 61900, max: 78900 }, { name: "睿行M60", min: 51900, max: 59900 }, { name: "睿行M70", min: 60500, max: 74000 }, { name: "睿行M80", min: 58500, max: 72000 }, { name: "睿行M90", min: 68500, max: 92500 }, { name: "神骐F30", min: 47600, max: 63800 }, { name: "神骐T10", min: 39900, max: 50900 }, { name: "神骐T20", min: 33900, max: 59700 }, { name: "凯程F70", min: 92800, max: 139800 }, { name: "长安星卡L系列", min: 39900, max: 47900 }, { name: "长安星卡C系列", min: 30900, max: 33600 }, { name: "睿行ES30", min: 66800, max: 69800 }, { name: "睿行EM60", min: 122800, max: 122800 }, { name: "睿行EM80", min: 96800, max: 120000 } ] }, { brandName: "长安跨越", models: [{ name: "长安跨越V3", min: 33500, max: 34300 }, { name: "跨越王X1", min: 39600, max: 51800 }, { name: "跨越王X3", min: 45700, max: 58400 }, { name: "长安跨越王X5", min: 47400, max: 65600 }, { name: "长安新豹2", min: 40700, max: 58600 }, { name: "新豹3", min: 46200, max: 59900 }, { name: "新豹T3", min: 41300, max: 51800 }, { name: "长安新豹MINI", min: 32100, max: 50500 }, { name: "跨越者D5", min: 59300, max: 67700 }, { name: "长安跨越新能源V3 EV", min: 79200, max: 79200 }, { name: "长安跨越新能源V5 EV", min: 93800, max: 93800 } ] }, { brandName: "力帆", models: [{ name: "迈威", min: 56800, max: 73800 }, { name: "力帆X80", min: 109900, max: 149900 }, { name: "力帆820", min: 76800, max: 119800 }, { name: "轩朗", min: 69800, max: 106800 }, { name: "乐途", min: 35800, max: 59800 }, { name: "力帆650EV", min: 168900, max: 175800 }, { name: "力帆820EV", min: 256800, max: 279800 } ] }, { brandName: "长城", models: [{ name: "风骏5", min: 68800, max: 112800 }, { name: "风骏6", min: 86800, max: 117800 }, { name: "风骏7", min: 86800, max: 138800 }, { name: "炮", min: 97800, max: 159800 }, { name: "长城C30 EV", min: 150000, max: 154000 } ] }, { brandName: "哈弗", models: [{ name: "哈弗H2", min: 74900, max: 95900 }, { name: "哈弗H2s", min: 70000, max: 85000 }, { name: "哈弗H4", min: 73900, max: 115000 }, { name: "哈弗F5", min: 100000, max: 130000 }, { name: "哈弗H5", min: 107800, max: 136800 }, { name: "哈弗M6", min: 66000, max: 82000 }, { name: "哈弗H6", min: 102000, max: 136000 }, { name: "哈弗H6 Coupe", min: 79900, max: 119000 }, { name: "哈弗H7", min: 142000, max: 180000 }, { name: "哈弗F7", min: 109000, max: 153700 }, { name: "哈弗F7x", min: 119900, max: 154900 }, { name: "哈弗H9", min: 209800, max: 272800 }, ] }, { brandName: "江铃", models: [{ name: "宝典", min: 78800, max: 105300 }, { name: "凯锐800", min: 120800, max: 129100 }, { name: "凯运强劲版", min: 99500, max: 117400 }, { name: "凯运升级版", min: 94800, max: 106800 }, { name: "顺达宽体", min: 90800, max: 100000 }, { name: "顺达窄体", min: 84000, max: 89000 }, { name: "特顺", min: 101300, max: 142700 }, { name: "域虎3", min: 89800, max: 122200 }, { name: "域虎5", min: 96800, max: 136300 }, { name: "域虎7", min: 119800, max: 176300 }, { name: "江铃E100B", min: 73800, max: 73800 }, { name: "江铃E160", min: 95800, max: 98800 }, { name: "江铃E200L", min: 87800, max: 90800 }, { name: "江铃E200N", min: 90800, max: 93800 }, { name: "易至EV3", min: 66800, max: 83800 }, { name: "易至EX5", min: 89800, max: 122800 }, { name: "特顺EV", min: 206000, max: 206000 }, { name: "骐铃T5", min: 65800, max: 86800 }, { name: "骐铃T7", min: 72800, max: 113800 }, { name: "骐铃T100", min: 53800, max: 71800 }, { name: "D-MAX房车", min: 283000, max: 396000 }, { name: "考斯特房车", min: 318000, max: 380000 }, { name: "旅居房车", min: 536000, max: 596000 }, { name: "罗莎房车租赁款", min: 268000, max: 288000 }, { name: "骐铃T7皮卡房车", min: 258000, max: 299800 }, { name: "全顺商旅房车", min: 468000, max: 518000 }, { name: "全顺T型房车", min: 408000, max: 448000 }, { name: "商旅房车", min: 438000, max: 488000 }, { name: "商旅房车经典款", min: 388000, max: 428000 }, { name: "途睿欧商务车", min: 298000, max: 398000 } ] }, ] }; var carTypeModelsCount = 0; GV_CarInfoInitConfig.carTypes.forEach((carType) => { carTypeModelsCount += carType.models.length; }); print("🚗🚗🚗共有不同品牌型号的汽车" + carTypeModelsCount + "种🚗🚗🚗"); print("🚗🚗🚗伪造数据,计时开始🚗🚗🚗"); var insertTotalDataCount = 0, startTimeForCarInfo = new Date(); var GV_TempCarLicensePlates = []; GV_CarInfoInitConfig.carTypes.forEach((carType) => { if (Array.isArray(carType.models) && carType.models.length) { carType.models.forEach((model) => { for (var i = 0; i < getRangeRandomNumber(GV_CarInfoInitConfig.carModelMin, GV_CarInfoInitConfig.carModelMax); i++) { // 这里会生成一条车辆信息记录(一辆真正的车,有车牌号) var tempCarInfoPlate = getRandomLicensePlate(GV_TempCarLicensePlates), tempCarInfoBuyDate = getRangeRandomDate("2010-01-01", "2018-06-06", "yyyy-MM-dd HH:mm:ss"); var trForCarInfo = Object.assign({ _id: getGUID(), FormItems: [{ key: "1572493551001", value: carType.brandName }, { key: "1572493551002", value: model.name }, { key: "1572493551003", value: tempCarInfoPlate }, { key: "1572493551004", value: tempCarInfoBuyDate }, { key: "1572493551005", value: getRangeRandomNumber(model.min, model.max) }, { key: "1572493551006", value: getCustomChineseCharacters(getRangeRandomNumber(200)) } ] }, getFormInstanceOtherAttrs(GV_CarInfoInitConfig.formId, GV_CarInfoInitConfig.formVersion)); var currentNewTrs = [trForCarInfo]; // 这里根据上面的车辆信息生成随机数量的车辆花费记录 var currentCarWastageCount = getRangeRandomNumber(GV_CarWastageInitConfig.carWastageMin, GV_CarWastageInitConfig.carWastageMax); for (var j = 0; j < currentCarWastageCount; j++) { var tempUserIdIndexWastageType = getRangeRandomNumber(GV_CreateUserIds.length - 1); var tempWastageTypeIndex = getRangeRandomNumber(GV_CarWastageInitConfig.wastageType.length - 1); var tempWastageTypeDetailIndex = getRangeRandomNumber(GV_CarWastageInitConfig.wastageType[tempWastageTypeIndex].detailed.length - 1); currentNewTrs.push(Object.assign({ _id: getGUID(), FormItems: [{ key: "1572493552001", value: { id: trForCarInfo._id, name: tempCarInfoPlate, value: "" } }, { key: "1572493552002", value: [{ id: GV_CreateUserIds[tempUserIdIndexWastageType], name: GV_CreateUserIds[tempUserIdIndexWastageType], face: "" } ] }, { key: "1572493552003", value: getRangeRandomDate(tempCarInfoBuyDate, "2019-11-07", "yyyy-MM-dd HH:mm:ss") }, { key: "1572493552004", value: GV_CarWastageInitConfig.wastageType[tempWastageTypeIndex].text }, { key: "1572493552005", value: getRangeRandomNumber(20, 10000) }, { key: "1572493552006", value: GV_CarWastageInitConfig.wastageType[tempWastageTypeIndex].detailed[tempWastageTypeDetailIndex] } ] }, getFormInstanceOtherAttrs(GV_CarWastageInitConfig.formId, GV_CarWastageInitConfig.formVersion))); } // 这里根据上面的车辆信息生成随机数量的车辆营收记录 var currentCarRevenueCount = getRangeRandomNumber(GV_CarRevenueInitConfig.carRevenueMin, GV_CarRevenueInitConfig.carRevenueMax); for (var k = 0; k < currentCarRevenueCount; k++) { var tempUserIdIndexRevenue = getRangeRandomNumber(GV_CreateUserIds.length - 1); var tempRevenueIndex = getRangeRandomNumber(GV_CarRevenueInitConfig.revenueTypes.length - 1); var tempRevenueDetailIndex = getRangeRandomNumber(GV_CarRevenueInitConfig.revenueTypes[tempRevenueIndex].detailed.length - 1); currentNewTrs.push(Object.assign({ _id: getGUID(), FormItems: [{ key: "1572493553001", value: { id: trForCarInfo._id, name: tempCarInfoPlate, value: "" } }, { key: "1572493553002", value: [{ id: GV_CreateUserIds[tempUserIdIndexRevenue], name: GV_CreateUserIds[tempUserIdIndexRevenue], face: "" } ] }, { key: "1572493553003", value: getRangeRandomDate(tempCarInfoBuyDate, "2019-11-07", "yyyy-MM-dd HH:mm:ss") }, { key: "1572493553004", value: GV_CarRevenueInitConfig.revenueTypes[tempRevenueIndex].text }, { key: "1572493553005", value: getRangeRandomNumber(20, 10000) }, { key: "1572493553006", value: getRangeRandomNumber(50, 3000) }, { key: "1572493553007", value: GV_CarRevenueInitConfig.revenueTypes[tempRevenueIndex].detailed[tempRevenueDetailIndex] }, ] }, getFormInstanceOtherAttrs(GV_CarRevenueInitConfig.formId, GV_CarRevenueInitConfig.formVersion))); } print("开始插入 车辆:" + carType.brandName + "->" + model.name + "->" + tempCarInfoPlate + " 本身和与之关联的" + GV_CarWastageInitConfig.formName + "、" + GV_CarRevenueInitConfig.formName + "的记录"); var pageNum = Math.ceil(currentNewTrs.length / pageSize); for (var m = 1; m <= pageNum; m++) { var currentPageTrs = currentNewTrs.slice((m - 1) * pageSize, m * pageSize); print(" 第" + m + "页,共" + currentPageTrs.length + "条"); realBatchInsertData(currentPageTrs); } insertTotalDataCount += currentNewTrs.length; print("结束插入 车辆:" + carType.brandName + "->" + model.name + "->" + tempCarInfoPlate + "->" + currentNewTrs.length + "条,累计:" + insertTotalDataCount + "条"); } }); } }); GV_TempCarLicensePlates.length = 0; GV_CarInfoInitConfig = null; GV_CarWastageInitConfig = null; GV_CarRevenueInitConfig = null; print("🚗🚗🚗伪造数据,计时结束🚗🚗🚗 总共插入:" + insertTotalDataCount + "条数据。" + "耗时:" + (new Date() - startTimeForCarInfo) / 1000 + "s");
【2021-09-23伪造数据代码更新】
2021-07-13的更新,语句只能在MongoDB服务器本地执行,如果想要远程执行呢?你可以在使用“db”之前添加下面的语句:
// https://stackoverflow.com/questions/26389424/cant-make-basic-mongo-shell-script-with-authentication var mongo = new Mongo('IP地址:端口'); mongo.getDB("admin").auth('用户名', '密码'); var db = mongo.getDB('数据库名称');