【发布时间】:2018-06-02 09:32:31
【问题描述】:
<?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"}}]}
【问题讨论】:
-
错误消息似乎没有正确解析 JSON。但根本没有必要这样做。 MongoDB 驱动程序实际上使用 PHP 数据结构。您似乎认为您需要 JSON 并将数据构建到 JSON 字符串中,但您根本不需要这样做。
-
请看编辑,那是给我错误的JSON,我觉得很好,应该改插入?所有其他人都被插入,我认为他们是平等的
-
请阅读评论。 不要构建 JSON 字符串。只需将 PHP 数据用作自然的
array()或[]结构并插入即可。 -
顺便说一句。您还使用了错误的驱动程序。当您真正想要的是 PHPLIB Driver 时,您正在尝试使用核心驱动程序,它与其他语言实现具有通用 API。您应该使用的方法是
MongoDB\Collection::bulkWrite()或者实际上在您的上下文中只是MongoDB::Collection::insertOne()