2012-03-31 58 views
2

我对PHP和MongoDB相当陌生,所以如果我从你们那里得到一些建议,这将非常有帮助。我一直在干这个循环好几个小时,但无济于事。编写一个MongoDB插入语句

这大致是我输入:

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++; 
} 

它输出:

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)) 

我有最后一个问题是实际上一切组合成一个INSERT语句而不是多重插入语句作为你看到上面。所以,而不是生成8插入语句,我试图让它做出1插入语句。

有什么建议吗?

回答

1

MongoDB没有事务处理,并且插入是快速和轻量级的,所以没有特别的理由尝试批量处理这么小的数量。虽然我以前没有使用它,但是您可以尝试使用BatchInsert method

+0

是的,我的代码需要一个输入的CSV文件,所以它也必须能够执行大量的记录。 – EGHDK 2012-03-31 21:00:05

+0

我尝试过使用BatchInsert方法,但它似乎没有将其改变为我所需要的。任何其他想法? – EGHDK 2012-04-01 02:23:41

+0

根据我的理解,您应该使用BatchInsert例程。我不清楚你有什么问题。 – gview 2012-04-02 21:47:36