我有一个应用程序,我正在尝试从旧版驱动程序更新为新驱动程序。触发以下代码时,我遇到了集合损坏的问题。我想我把范围缩小了。
function update($collection,$criteria,$data,$insertIfNotExists = false)
{
if(!empty($collection) && !empty($criteria) && !empty($data)) {
if (!isset($this->collection[$collection])) {
$this->collection[$collection] = (new MongoDB\Client)->hebe->{$collection};
}
if ($insertIfNotExists) {
$oldData = $this->collection[$collection]->findOne($criteria);
if ($oldData == NULL) {
$data['createdDate'] = date("Y-m-d H:i:s");
$data['modifiedDate'] = (isset($data['modifiedDate'])) ? $data['modifiedDate']:date("Y-m-d H:i:s");
/*
return ($this->collection[$collection]->insertOne($data)) ? array('status'=>'ok'):array('status'=>'error','error'=>'unknown_error');
*/
} else {
$newData = $oldData;
foreach($data as $n=>$v) {
$newData[$n] = $v;
}
$newData['modifiedDate'] = (isset($newData['modifiedDate'])) ? $newData['modifiedDate']:date("Y-m-d H:i:s");
/*
return ($this->collection[$collection]->updateOne($criteria,['$set' => $newData])) ? array('status'=>'ok'):array('status'=>'error','error'=>'unknown_error');
*/
}
} else {
/*
return ($this->collection[$collection]->updateOne($criteria,['$set' => $data])) ? array('status'=>'ok'):array('status'=>'error','error'=>'unknown_error');
*/
}
}
}
示例变量是
$collection = 'customer'
$criteria = array ( 'number' => '9999',)
$data = array (
'number' => '9999',
'name' => 'Testing Account',
'reference' => 'Peter Smith',
'defaultDeliveryAddress' => 1,
'visitAddress' => '',
'address' => '',
'district' => 'Marknad',
'postAddress' => '',
'orgNumber' => '5562041771',
'phone' => '031-7802700',
'fax' => '031-193328',
'groupCode' => 'int',
'creditCustomer' => '',
'typeOfDelivery' => 'Bil',
'typeOfPayment' => '10',
'emailAddresses' =>
array (
'invoice' => 'email1@domain.com',
'order' => 'email2@domain.com',
'deliveryNote' => 'email3@domain.com',
'packingSlip' => 'email4@domain.com',
),
'orderType' => NULL,
'termsOfDelivery' => 'RC',
'creditLimit' => 100000.0,
'pricelist' => 4,
'pricelist1' => '',
'pricelist2' => '9998',
'pricelist3' => '104',
'discount' => NULL,
'countryCode' => 'SE',
'currencyCode' => 'SEK',
'blocked' => 0,
'deliveryCost' => 0,
'vatCode' => '2',
'email' => 'peremail@domain.com',
'password' => 'password',
'modifiedDate' => '2019-06-25 00:00:00',
'deliveryAddresses' =>
array (
0 =>
array (
'row' => 1,
'name' => 'Test Address',
'address' => 'Box 12345',
'postAddress' => '42246',
'default' => true,
),
1 =>
array (
'number' => '9999',
'name' => 'Testing Address',
'reference' => '13232',
'address' => 'Box 12245',
'postAddress' => '42246',
),
),
'references' =>
array (
0 =>
array (
'number' => '9999',
'name' => 'Testing',
'email' => '',
'password' => '',
),
1 =>
array (
'number' => '9999',
'name' => 'Testing2',
'email' => '',
'password' => 'password',
),
2 =>
array (
'number' => '9999',
'name' => 'Peter Smith',
'email' => '',
'password' => 'password',
),
),
)
有人可以针对我在updateOne
和insertOne
上做错的事情为我指明正确的方向。据我了解,文档支持数组。
编辑:有一点背景是我将该系统和MongoDB从2.6升级到了3.6。我还从旧版mongo升级了mongodb驱动程序。
答案 0 :(得分:0)
问题在于集合中的旧数据带有引号,而updateOne / insertOne数据则没有。更正此问题似乎可以解决问题。