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!
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.
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
bulkWriteNow 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.