先随机生成json数据文件
<?php
set_time_limit(0);
$fp =fopen('userinfos.json','w');
fclose($fp);
$fp =fopen('userinfos.sql','w');
fclose($fp);
$maxNum = 1000000;
$sql = "TRUNCATE TABLE `user`;\r\n";
for($i=1; $i < $maxNum; $i++)
{
$userInfo = array(
'name' => uniqid(),
'sex' => rand(0,1),
'avatar' => rand(1,300),
'level' => rand(1,150),
'friendNum' => rand(1,1000),
'exp' => rand(1,200000),
'gold' => rand(1,200000),
'cash' => rand(0,150),
);
$json = json_encode($userInfo);
$json .= "\r\n";
file_put_contents('userinfos.json',$json,FILE_APPEND);
if($i == 1)
{
$sql = "INSERT INTO `user`(`".implode("`,`",array_keys($userInfo))."`)VALUES";
}
else
{
$dott = (($i + 1) == $maxNum) ? ";" : ",";
$sql = "(`".implode("`,`",array_values($userInfo))."`)".$dott."\r\n";
}
file_put_contents('userInfos.sql',$sql,FILE_APPEND);
}
?>
然后进入mongodb的bin目录执行如下命令
mongoimport -d test -c user --drop E:\xampp\htdocs\userinfos.json