note 111913 added to mongocollection.save

From: Date: Wed, 10 Apr 2013 21:13:45 +0000
Subject: note 111913 added to mongocollection.save
Groups: php.notes 
Request: Send a blank email to php-notes+get-194437@lists.php.net to get a copy of this message
I noticed that when my object included an array that I didn't want to write to MongoDB as an associated array, I had to use json_encode and json_decode to get the desired results. Example: <?php $m = new Mongo(MONGOSERVER); $db = $m->testdb; $collection = $db->test; $object = new testobj(); $collection->save($object); class testobj { var $foo; var $tags; var $associated; function __construct() { $this->foo = 'bar'; // no keys for tags array $this->tags = json_encode(array('simple','dimple','thimble')); // keys for associated array $this->associated = array('simple','dimple','thimble'); } } ?> Result stored in MongoDB: [_id] => MongoId Object ( [$id] => 5165d41019c2f59614000000 ) [foo] => bar [tags] => ["simple","dimple","thimble"] [associated] => Array ( [0] => simple [1] => dimple [2] => thimble ) Then when retrieving, json_decode() on the tags is useful if you want to do a foreach() across the tags array. There may be a more elegant solution, but this was the best I could come up with! ---- Server IP: 69.147.83.199 Probable Submitter: 108.234.0.62 ---- Manual Page -- http://php.net/manual/en/mongocollection.save.php Edit -- https://master.php.net/note/edit/111913 Del: integrated -- https://master.php.net/note/delete/111913/integrated Del: useless -- https://master.php.net/note/delete/111913/useless Del: bad code -- https://master.php.net/note/delete/111913/bad+code Del: spam -- https://master.php.net/note/delete/111913/spam Del: non-english -- https://master.php.net/note/delete/111913/non-english Del: in docs -- https://master.php.net/note/delete/111913/in+docs Del: other reasons-- https://master.php.net/note/delete/111913 Reject -- https://master.php.net/note/reject/111913 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194437) next »