PHP/MongoDB - Add subdocument to document with _id -
i trying learn mongodb php. can save new document ->save($object). goal add subdocument document specific _id.
i have document:
"5197f4bef045a1d710000032": { "_id": { "$id": "5197f4bef045a1d710000032" }, "tag": 5487, "serial": "1z5589ss56", "type": "soda can", "dept": "noc", "location": "mitchell", "date": 1368913086, "moves": null }
i insert new document "moves".
i have been able once $set, subsequent $push nothing.
<?php $m = new mongo(); $d = $m->selectdb('peeps'); $c = $d->family; $fields = array('tag' => 5487 , 'serial' => '1z5589ss56', 'type' => 'soda can', 'dept' => 'noc', 'location' => 'mitchell', 'date' => time() ); $can = new asset($fields); #$c->save($can); #update insert move $c->update(array('_id' => new mongoid('5197f0cef045a1d710000032')), array('$push' => array('moves' => $can))); $cur = $c->find(); $j = json_encode(iterator_to_array($cur)); print($j); class asset{ public $tag; public $serial; public $type; public $dept; public $location; public $date; public $moves; public function __construct($fields){ $this->tag = $fields['tag']; $this->serial = $fields['serial']; $this->type = $fields['type']; $this->dept = $fields['dept']; $this->location = $fields['location']; $this->date = $fields['date']; } }
based on have read, should create nested document inside 5197f0cef045a1d710000032.moves every time reload page.
i not sure missing.
thanks,
mitchell
you should not using $set simple value if plan on later using $push
same field.
use $push
first time update also. create array of single element, , able add (push) more elements it.
Comments
Post a Comment