0

I am using MongoDB in my symfony project. For communication to the database, i am using either Document Manager and MongoClient.

What i intend to do is to batch insert an array directly into the database. I've made attempt of creating array and fill it. However when i execute batchInsert(), i get this exception

[MongoException]    
 no documents given

Very confusing because according to the manual, the exception thrown if the inserted array is empty. I've made sure and just tried many times that the array i was inserting is not empty. This is my piece of code:

$dm = $this->getContainer()->get('doctrine_mongodb');

            echo "initiating... \n";

        $data = $dm->getRepository("KahviDocumentBundle:Suppliers")->findAll();

        $children = array();

        echo "fetched\n";

        echo "\n".count($data)."\n";

        $items = array();
        foreach ($data as $row) {
            foreach ($row->getValue()->getConsigneeName() as $consignee) {

                    $items[] = array(
                        'id' => Util\Util::slugify($consignee),
                        'name' => $consignee,
                        'data' => array(
                            'band' => $row->getValue()->getShipperName(),
                            'relation' => 'Buyer of band'
                        )
                    );

            }
            // echo $row->getValue()->getShipperName()."\n";
        }
        echo "items collected\n".count($items)."\n";


        foreach ($items as &$item) {
            $parent = $item['data'];
            $children[Util\Util::slugify($parent['band'])][] = &$item;
        }

        unset($item);

        foreach ($items as &$item) {
            if (isset($children[$item['id']])) {
                $item['children'] = $children[$item['id']];
            }

        }
        unset($item);

        // die();
        echo "start deleting\n";
        $mongo = new \MongoClient('mongodb://192.168.125.17:27017');

        $db = $mongo->selectDB('dbname');
        // print_r($db);
        $collection = $db->the_collection;
        // print_r($collection);
        $response = $collection->drop();

        echo "finished\n";
        print_r($response);
        // die();

        echo "starting recording\n";

        $newCollection = $db->createCollection(
            "the_collection",
            array(
                'capped' => true,
                'size' => 1024*1024,
                'max' => 10
            )
        );

        echo "children count ".count($children)."\n";

        $trees = array();
        foreach ($children as $key => $child) {

            $test = array(
                'id' => $key,
                'name' => $key,
                'children' => is_array($child) ? $child : array()
            );
            $serialized = serialize($test);
            $trees[] = $serialized;
            // echo $serialized."\n";
        }
        // die();
        var_dump($trees);
        $newCollection->batchInsert($trees);

        // $dm->getManager()->flush();

        echo "finished\n";
4

1 回答 1

0

您已经插入了一个字符串数组,而预期的类型是一个数组的数组,其中内部数组是一组列(字段)而且,您不需要重新创建您删除它的同一个集合。

所以应该是这样的。

$db = $mongo->selectDB('dbname');
        $collection = $db->the_collection;
        $collection->drop();    

$trees = array();
            foreach ($children as $key => $child) {
            $test = array(
                'id' => $key,
                'name' => $key,
                'children' => is_array($child) ? $child : array()
            );
            // echo print_r($test);
            // echo "\n";
            $serialized = serialize($test);
            $trees[] = array('column1' => $serialized, 'column2'=> $key);
        }

        $collection->batchInsert($trees);
于 2013-10-12T07:09:07.873 回答