我在 Laravel 5 中使用 jenssegers 包来表示 mongodb。我以下面描述的方式插入多个数据,数据已成功插入 mongodb 中,但在脚本完成之前通过错误。
$AllTrans=array();
$AllTrans[]=array("InvoiceID"=>1,"Amount"=>50);
$AllTrans[]=array("InvoiceID"=>2,"Amount"=>150);
$mongo_connnection->collection('invoices')->insert($AllTrans);
这是错误:
MongoException in Collection.php line 42:
No write ops were included in the batch
但是我无法找出问题,我已经尝试使用插入查询传递数组('multi' => true)等选项,但它不起作用。
它可以很好地与批量添加一起使用,这样,您只需要在数组上创建并将其传递给它。
$temp = [
[
'item'=> "envelopes"
],
[
'item'=> "envelopesas"
],
[
'item'=> "lala"
]
];
$userData = DB::table('log')->raw( function ( $collection ) use ($temp) {
return $collection->insertMany($temp);
});