<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
try {
$link = mysqli_connect('localhost', 'root', '')
or die('No se pudo conectar: ' . mysqli_error());
mysqli_select_db($link,'classicmodels5') or die('No se pudo seleccionar la base de datos');
$query = "select c.customerNumber,c.customerName,c.city as customerCity, c.country as customerCountry,c.salesRepEmployeeNumber from customers as c";
$result = mysqli_query($link,$query);
$isFirst=0;
$isFirst2=0;
$isFirst3=0;
$isFirst4=0;
$json="";
while ($fila = mysqli_fetch_array($result)) {
$bulk = new MongoDB\Driver\BulkWrite;
$isFirst2=0;
$isFirst3=0;
$isFirst4=0;
extract($fila);
$json = $json."{";
//$json = $json.",{";
$json = $json.'"customerNumber":'.$fila["customerNumber"].',';
$json = $json.'"customerName":"'.$fila["customerName"].'",';
$json = $json.'"city":"'.$fila["customerCity"].'",';
$json = $json.'"country":"'.$fila["customerCountry"].'"';
//INICIO:INSERTAR PAYMENTS
$query2 = "select p.checkNumber,p.amount from payments as p where p.customerNumber=".$fila["customerNumber"];
$result2 = mysqli_query($link,$query2);
if(mysqli_num_rows($result2)>0){
$json=$json.',"payments":[';
}
while ($fila2 = mysqli_fetch_array($result2)) {
extract($fila2);
if($isFirst2==1){
$json = $json.",{";
}else{
$json = $json."{";
}
$json = $json.'"checkNumber":"'.$fila2["checkNumber"].'",';
$json = $json.'"amount":"'.$fila2["amount"].'"}';
$isFirst2=1;
}
if(mysqli_num_rows($result2)>0){
$json=$json.']';
}
//FIN:INSERTAR PAYMENTS
//INICIO: INSERTAR EMPLOYEE
$query3 = "SELECT e.employeeNumber,e.lastName,e.extension,e.officeCode FROM employees as e where e.employeeNumber=".$fila["salesRepEmployeeNumber"];
$result3 = mysqli_query($link,$query3);
if(mysqli_num_rows($result3)>0){
$json=$json.',"employees":[';
}
while ($fila3 = mysqli_fetch_array($result3)) {
extract($fila3);
if($isFirst3==1){
$json = $json.",{";
}else{
$json = $json."{";
}
$json = $json.'"employeeNumber":"'.$fila3["employeeNumber"].'",';
$json = $json.'"lastName":"'.$fila3["lastName"].'",';
$json = $json.'"extension":"'.$fila3["extension"].'"';
//INICIO:INSERTAR OFFICE
$query4 = "SELECT o.officeCode,o.city,o.country FROM offices as o where o.officeCode=".$fila3["officeCode"];
$result4 = mysqli_query($link,$query4);
if(mysqli_num_rows($result4)>0){
$json=$json.',"officeCode":';
}
while ($fila4 = mysqli_fetch_array($result4)) {
extract($fila4);
if($isFirst4==1){
$json = $json.",{";
}else{
$json = $json."{";
}
$json = $json.'"officeCode":"'.$fila4["officeCode"].'",';
$json = $json.'"city":"'.$fila4["city"].'",';
$json = $json.'"country":"'.$fila4["country"].'"}';
$isFirst4=1;
}
//FIN:INSERTAR OFFICE
$isFirst3=1;
}
if(mysqli_num_rows($result3)>0){
$json=$json.'}]';
}
//FIN:INSERTAR EMPLOYEE
$json = $json.'}';
echo $json."</br>";
//Hacer el insert
$bulk->insert(json_decode($json));
$resultFinal = $manager->executeBulkWrite('test.prueba3', $bulk);
$json="";
$isFirst=1;
}
$json = str_replace("'", " ", $json);
echo $json;
//echo $json;
/* $bulk->insert(json_decode($json));
$resultFinal = $manager->executeBulkWrite('test.customers', $bulk);
var_dump($resultFinal);*/
}catch(Exception $e) {
echo "EXCEPTION: ".$e->getMessage(), "\n";
exit;
}
?>
当我执行插入操作时,每次完成第一个循环时,我都会收到以下错误,我不明白是否因为JSON错误或应该以其他方式插入
Warning: MongoDB\Driver\BulkWrite::insert() expects parameter 1 to be array, null given in C:\xampp2\htdocs\prac\index.php on line 111
EXCEPTION: Cannot do an empty bulk write
我需要每次完成第一个循环插入JSON文档,在下一圈添加另一个,我得到了错误但是在JSONLint中验证我的json时,我认为这是有效的。是从MySQL迁移到MongoDB。
这是给出错误的JSON,我认为这是正确编写的,您认为这个错误在哪里?谢谢
{"customerNumber":144,"customerName":"Volvo Model Replicas, Co","city":"Lule�","country":"Sweden","payments":[{"checkNumber":"IR846303","amount":"36005.71"},{"checkNumber":"LA685678","amount":"7674.94"}],"employees":[{"employeeNumber":"1504","lastName":"Jones","extension":"x102","officeCode":{"officeCode":"7","city":"London","country":"UK"}}]}
答案 0 :(得分:0)
我将您的代码简化为:
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
try {
$json='{"customerNumber":144,"customerName":"Volvo Model Replicas, Co","city":"Lule�","country":"Sweden","payments":[{"checkNumber":"IR846303","amount":"36005.71"},{"checkNumber":"LA685678","amount":"7674.94"}],"employees":[{"employeeNumber":"1504","lastName":"Jones","extension":"x102","officeCode":{"officeCode":"7","city":"London","country":"UK"}}]}';
$bulk = new MongoDB\Driver\BulkWrite;
echo $json."</br>";
//Hacer el insert
$bulk->insert(json_decode($json));
$resultFinal = $manager->executeBulkWrite('test.prueba3', $bulk);
$json="";
$isFirst=1;
}catch(Exception $e) {
echo "EXCEPTION: ".$e->getMessage(), "\n";
exit;
}
打印JSON并保存数据做mongodb
> db.prueba3.find().pretty()
{
"_id" : ObjectId("5b13bbda65b9bb1f3e6b9922"),
"customerNumber" : 144,
"customerName" : "Volvo Model Replicas, Co",
"city" : "Lule�",
"country" : "Sweden",
"payments" : [
{
"checkNumber" : "IR846303",
"amount" : "36005.71"
},
{
"checkNumber" : "LA685678",
"amount" : "7674.94"
}
],
"employees" : [
{
"employeeNumber" : "1504",
"lastName" : "Jones",
"extension" : "x102",
"officeCode" : {
"officeCode" : "7",
"city" : "London",
"country" : "UK"
}
}
]
}
请确认此示例适用于您的情况。如果不是,您可以在评论$json
之前比较此示例中定义的$json
和//Hacer el insert
。
如果有可能,请解决您的问题。包括如此大量的代码并不像小的那么有用。为了帮助其他人做出响应,您可以使用示例数据,而不是使用数据库中的选择来粘贴代码。
从字符串连接JSON是非常糟糕的做法。想象一下,你的一个字符串包含这样的字符:&#34; {&#34;。你逃脱了吗?
可能与编码Lule�
有关的潜在问题。您正在使用Windows,可能您的代码文本或SQL数据库的编码与UTF-8不同。