EGHDK
EGHDK

Reputation: 18120

Writing a MongoDB insert statement

I'm fairly new to PHP and MongoDB so it would be really helpful if I got some suggestions from you guys. I've been messing with this loop for hours on end, but to no avail.

This is roughly my input:

while ($currentCol<$maxCols){
    $obj = array( $currentarray[0][$currentCol] => $currentarray[$currentRow][$currentCol]);
    $collection->insert($obj);
    echo $testing;
    echo "<br />";
    print_r ($obj);
    echo "<br />";
    $testing++;
    $currentCol++;
}

It outputs:

1
Array ( [President ] => George Washington [_id] => MongoId Object ( [$id] => 4f774d924f62e5ca37000160 ) ) 
2
Array ( [Wikipedia Entry] => http://en.wikipedia.org/wiki/George_Washington [_id] => MongoId Object ( [$id] => 4f774d934f62e5ca37000161 ) ) 
3
Array ( [Took office ] => 30/04/1789 [_id] => MongoId Object ( [$id] => 4f774d934f62e5ca37000162 ) ) 
4
Array ( [Left office ] => 4/03/1797 [_id] => MongoId Object ( [$id] => 4f774d934f62e5ca37000163 ) ) 
5
Array ( [Party ] => Independent [_id] => MongoId Object ( [$id] => 4f774d934f62e5ca37000164 ) ) 
6
Array ( [Portrait] => GeorgeWashington.jpg [_id] => MongoId Object ( [$id] => 4f774d934f62e5ca37000165 ) ) 
7
Array ( [Thumbnail] => thmb_GeorgeWashington.jpg [_id] => MongoId Object ( [$id] => 4f774d934f62e5ca37000166 ) ) 
8
Array ( [Home State] => Virginia [_id] => MongoId Object ( [$id] => 4f774d934f62e5ca37000167 ) ) 

The last problem I'm having is to actually combine everything into one insert statement instead of having multiple insert statements as you see above. So, instead of generating 8 insert statements, I'm trying to get it to make 1 insert statement.

Any suggestions?

Upvotes: 2

Views: 272

Answers (1)

gview
gview

Reputation: 15361

MongoDB does not have transactions, and inserts are fast and lightweight, so there is no particular reason to try and batch them for such a small number. While I have not used it previously, there is however a BatchInsert method you can try out.

Upvotes: 1

Related Questions