note 115815 added to mongocollection.update

From: Date: Sun, 28 Sep 2014 07:08:01 +0000
Subject: note 115815 added to mongocollection.update
Groups: php.notes 
Request: Send a blank email to php-notes+get-200540@lists.php.net to get a copy of this message
A need for an app was to add a new entry to the industries collection if it didn't already exist, and pass that "string" _id back to the caller to add it to the businesses collection (as a string also - database design by others.... :)) function GetIndustryID($db, $industry_name_eng, $industry_name_rus) { // $db is the MongoDB database object // $industry_name_eng is English of the industry name // $industry_name_rus is Russian of the industry name if ( $industry_name_eng == "" && $industry_name_rus == "" ) { return ""; exit; } // lookup russian version // of industry name from industries collection and try to return the _id // if not found, add this as a new document and return its _id // $c_ind = industry collection object // "industries" is the collection name $c_ind = $db->industries; // you could use findOne here.... $cursor = $c_ind->find(array('industry_name.russian' => $industry_name_rus)); if ( $cursor->hasNext() ) { foreach ( $cursor as $document ) { $id = $document['_id']; } } else { // not found here - go add it $document = array( "industry_id" => "", "industry_name" => array("english" => $industry_name_eng, "russian" => $industry_name_rus ) ); $db->industries->insert($document); $id = $document['_id']; // update the industry_id in the industries collection // with the id returned - because we just added it, and // we need the industry_id the same as the _id // see: http://php.net/manual/en/mongocollection.update.php // update ( array $criteria , array $new_object [, array $options = array() ] ) // note how I cast the MongoID "object" to a string $document['industry_id'] = (string)$id; // the $id in the next line is the ObjectID, not a string. $db->industries->update(array("_id" => $id), $document, array("upsert" => true)); } return (string)$id; } Results: (using RoboMongo) { "_id" : ObjectId("5427acf7a495dbd420b2e2ea"), "industry_id" : "5427acf7a495dbd420b2e2ea", "industry_name" : { "english" : "Oil and Gas", "russian" : "Нефть, Газ, Энергетика" } } It took me ages to work this out. Hope it helps someone else. ---- Server IP: 72.52.91.14 Probable Submitter: 101.162.19.219 ---- Manual Page -- http://php.net/manual/en/mongocollection.update.php Edit -- https://master.php.net/note/edit/115815 Del: integrated -- https://master.php.net/note/delete/115815/integrated Del: useless -- https://master.php.net/note/delete/115815/useless Del: bad code -- https://master.php.net/note/delete/115815/bad+code Del: spam -- https://master.php.net/note/delete/115815/spam Del: non-english -- https://master.php.net/note/delete/115815/non-english Del: in docs -- https://master.php.net/note/delete/115815/in+docs Del: other reasons-- https://master.php.net/note/delete/115815 Reject -- https://master.php.net/note/reject/115815 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#200540) next »