Search code examples
mongodbphp-mongodbfuelphp

MongoDB (via Fuelphp): Adding entries on an Array


I would appreciate the help here. For the purpose of this discussion I have an example here (I will use paste bin for the codes):

http://pastebin.com/VPuyKn6W

I am trying to produce this output:

http://pastebin.com/4iMLacRu

I understand that I need to use $push to make this work. But upon testing, it doesn't seem to do anything. I am following the instructions as prescribed in the docs, but instead of using $Id, I am using user_id for finding the document in the collection. Here is my model:

http://pastebin.com/QB94tbZn

Am I misunderstanding something, or I am not using the $push operator properly, or something to do on how I created the document?


Solution

  • After walking outside, I finally got my answer.

        public static function create_mongo()
    {
        $data = array(
            'user_id'       => '123895',
            'First_Name'    => 'John',
            'Last_name'     => 'Doe',
            'sites'      => array(
                array(
                    'title' => 'Sankaku Complex',
                    'site'  => 'http://sankakucomples.com'
                )
            )
        );
    
        $db = Fuel\Core\Mongo_Db::instance();
        $db->insert('test_collection',$data);
    }
    

    sites should be an array carrying an array variable.