Laravel-mongodb: Support for executing bulk writes

Created on 10 Feb 2016  路  8Comments  路  Source: jenssegers/laravel-mongodb

As of MongoDB 3.2, db.collection now supports batch/bulk write operations (insert/update/remove): https://docs.mongodb.org/manual/reference/method/db.collection.bulkWrite/#db.collection.bulkWrite

Can support for this be added to the laravel-mongodb driver? This would greatly save on roundtrip performance for when there are a large number of batch updates in sequence.

I believe the PHP MongoDB driver already has support for executing bulk writes: http://php.net/manual/en/class.mongodb-driver-bulkwrite.php

Thanks!

Most helpful comment

@davidchchang This is already possible, I'm already using the bulkwrite from version 3.2 in some imports scripts I wrote.

First I create a new connection
new Jenssegers\Mongodb\Connection(\Config::get('database.connections.mongodb'))
Then i select the collection like so: $this->collection = $this->connection->getMongoDB()->selectCollection('collection');

This will give me a MongoDB collection in return and on that collection I can call bulkWrite

Now this isn't using the this library, except for the connection, but should enable you to write bulk imports :)

And personally when importing in bulk I'd rather be as close to the driver as possible as to not cause any unnecessary overhead.

All 8 comments

Pull requests are appreciated :)

@davidchchang This is already possible, I'm already using the bulkwrite from version 3.2 in some imports scripts I wrote.

First I create a new connection
new Jenssegers\Mongodb\Connection(\Config::get('database.connections.mongodb'))
Then i select the collection like so: $this->collection = $this->connection->getMongoDB()->selectCollection('collection');

This will give me a MongoDB collection in return and on that collection I can call bulkWrite

Now this isn't using the this library, except for the connection, but should enable you to write bulk imports :)

And personally when importing in bulk I'd rather be as close to the driver as possible as to not cause any unnecessary overhead.

It will work great with bulk addition, this way, you just need to create on array and pass it to it.

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

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

            return $collection->insertMany($temp);
        });

any update on this. Has anybody actually done this?

using php mongo library:https://github.com/mongodb/mongo-php-library
we need run :
composer require mongodb/mongodb
and then :

   ```
    use MongoDB\Client;

    $mongoClient=new Client();
    $mongodata=$mongoClient->mydb->mycol;
    $mongodata->insertMany($array);

```

How can i use updateMany? any ideas guys

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

$userData = DB::connection('mongodb')->collection('your_collection')->raw( function ( $collection ) use ($temp ) { return $collection->insertMany($temp ); });

Works for me

using php mongo library:https://github.com/mongodb/mongo-php-library
we need run :
composer require mongodb/mongodb
and then :

   ```
    use MongoDB\Client;

    $mongoClient=new Client();
    $mongodata=$mongoClient->mydb->mycol;
    $mongodata->insertMany($array);

This is not using the bulkWrite as @davidchchang was asking, you are referring to the insertMany command.

In the end I just retrieved the underlying connection and used that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lgt picture lgt  路  3Comments

HassanIbrahim picture HassanIbrahim  路  3Comments

sanjay1688 picture sanjay1688  路  3Comments

yupangestu picture yupangestu  路  3Comments

tomartailored picture tomartailored  路  3Comments