Skip to content
Advertisement

Insert multiple documents in mongodb collection with a single insert statement using php

In PHP I have some data to insert. The sample data is shown below

Array
(
    [0] => Array
        (
            [uuid] => membership-60f929a1989d6960400020
            [channel] => channel-594b9e3568ac3969784876
            [user] => user-5b1e6e1b636e5325325732
            [emailnotifications] => off
            [email] => demo@xyz.com
        )

    [1] => Array
        (
            [uuid] => membership-60f929a198a1a549293025
            [channel] => channel-594b9edcdea14894901526
            [user] => user-5b1e6e1b636e5325325732
            [emailnotifications] => off
            [email] => demo@xyz.com
        )

    [2] => Array
        (
            [uuid] => membership-60f929a198a2b975658255
            [channel] => channel-5ab261a60fe87688298186
            [user] => user-5b1e6e1b636e5325325732
            [emailnotifications] => off
            [email] => demo@vizsafe.com
        )

    [3] => Array
        (
            [uuid] => membership-60f929a198a3b292013762
            [channel] => channel-5e668d5522526659309093
            [user] => user-5b1e6e1b636e5325325732
            [emailnotifications] => off
            [email] => demo@vizsafe.com
        )

    [4] => Array
        (
            [uuid] => membership-60f929a198a4a688473796
            [channel] => channel-5e668d6fafbbe937126299
            [user] => user-5b1e6e1b636e5325325732
            [emailnotifications] => off
            [email] => demo@vizsafe.com
        )    
   

)

I would like to insert each element of the array as single document. For example :-

Array (
                [uuid] => membership-60f929a1989d6960400020
                [channel] => channel-594b9e3568ac3969784876
                [user] => user-5b1e6e1b636e5325325732
                [emailnotifications] => off
                [email] => demo@xyz.com
       )

should be inserted as a separate document . This should be achieved using a single insert statement . Also the version of mongodb being used is 2.4 so I cannot use insertMany. Can this be done ? Any leads would be helpful. Thanks for your response in advance.

Advertisement

Answer

update

It’s not possible for v2.4.

The batch insert was added to the PHP mongo driver in v1.5.0 combined with the switch to mongodb v2.6 write protocol:

It supports all new features for MongoDB 2.6, including:

  • Aggregate can now return a cursor
  • Aggregation pipelines can now be explained
  • Possible to set maxTimeMS for commands and queries
  • Transparent support for the new command-based MongoDB write API
  • New MongoWriteBatch classes (using the new MongoDB write API)

Previous answer:

The question is about legacy driver https://pecl.php.net/package/mongo/1.5.1

You need to use MongoInsertBatch to insert multiple documents.

From https://www.php.net/manual/en/class.mongoinsertbatch.php (doesn’t exist any more):

$docs = array();
$docs[] = array("my" => "demo");
$docs[] = array("is" => "working");
$docs[] = array("pretty" => "well");

$batch = new MongoInsertBatch($collection);
foreach($docs as $document) {
    $batch->add($document);
}
$retval = $batch->execute(array("w" => 1));

In your case $collection is $this->db->memberships

As a side note, you really need to upgrade the stack. It’s a decade old.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement