Laravel 5:在mongo DB中批量插入显示错误

时间:2016-02-02 11:28:24

标签: php mongodb laravel-5

我在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

但我无法弄清楚问题,我尝试使用插入查询传递像array('multi'=> true)这样的选项,但它无效。

1 个答案:

答案 0 :(得分:-1)

它可以很好地添加批量,这样,您只需要在数组上创建并将其传递给它。

$temp = [
            [
                'item'=> "envelopes"
            ],
            [
                'item'=> "envelopesas"
            ],
            [
                'item'=> "lala"
            ]
        ];

        $userData = DB::table('log')->raw( function ( $collection ) use ($temp) {

            return $collection->insertMany($temp);
        });
相关问题