📜 ⬆️ ⬇️

MongoDB tree storage

There are several algorithms for storing trees in mysql, and it seems to me that this is fundamentally wrong, since the relational database is not designed for this, and the representation of linear data structures in a tree is one big crutch in the code and a decrease in the reliability and speed of the application. How did I think about using NoSQL in particular mongoDB? It's simple, I was asked for an interview:
How to build a tree?

I replied:
We take mongodb and a document with a tree from it

I never got a call back, but I don’t get upset because now I’m learning mongoDB and I don’t like Mysql (fortunately or unfortunately I don’t know). In general, let us analyze how it all works? On the example of the comment tree written in php.

Immediately give the code:

//     mongodb     //       /** * @return \MongoDB\Driver\Manager * */ static function getConnect() { if(!is_null(self::$_connect)) { return self::$_connect; } self::$_connect = new \MongoDB\Driver\Manager(Config_Db::getConf()['mongodb']['connect']); return self::$_connect; } //   /** * add comment action */ public function addCommentAction() { $time = time(); //     //insert new comment to the page $arrData = array( 'page' => $_POST['page_id'], // id   mongo 'time' => $time, //    'name' => $_POST['name'], //   'comment' => $_POST['comment'] //   ); $connect = Core_Model_Mongo::getConnect(); //    //              //           $write = new MongoDB\Driver\BulkWrite(); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY); //  ,     - ,     reply  id  if(isset($_POST['reply']) && !empty($_POST['reply'])) { // id  $reply = $_POST['reply']; //     //  ,        $path = ''; if(isset($_POST['path']) && !empty($_POST['path'])) { $path = $_POST['path']; } else { $path = 'replies'; } // ,     $write->update( array('_id' => new MongoDB\BSON\ObjectID($reply)), //   array('$push' => array($path => $arrData) ) //  push,   ,  path ); } else { $write->insert($arrData); //       } //   $connect->executeBulkWrite(Config_Db::getConf()['mongodb']['db'] . '.comments', $write, $writeConcern); //    header('Location:' . $_POST['back_url']); } 

And so the path variable is passed to the path variable, for example, we have a comment in Mongo, we represent it in the form of a 'pure' array, since in reality this array of objects we simplify everything to an array.

One comment array
  array ( 'name' => 'test', 'comment' => 'test comment', ); 


After update, with push and the path = 'reply' variable, our array will take the form:
')
Array after push
  array ( 'name' => 'test', 'comment' => 'test comment', 'reply' => array ( [0] => array( 'name' => 'test reply test' 'comment' => 'test comment reply' ) ) ); 


And if you respond to a nested comment, the path will take the form of 'reply.0.reply', that is, in the comment reply, take 0 element, and insert a new reply field with the data, after the operation we will get the following array.

Array with a replica to the nested comment
  array ( 'name' => 'test', 'comment' => 'test comment', 'reply' => array ( [0] => array( 'name' => 'test reply test' 'comment' => 'test comment reply' 'reply' => array ( [0] => array( 'name' => 'test reply test' 'comment' => 'test comment reply' ) ) ) ) ); 


And this is all, the whole algorithm, just insert and then just take this array for the render so it was, is and will be. Render will not paint as the render is not such an important part, and I just do it through print_r () a joke of course, but the principle is the same ... Thank you for your attention dear habouriors.

PS: In the example, the algorithm is presented and there are assumptions in it, namely the absence of sorting comments and its persistence.

Source: https://habr.com/ru/post/279915/


All Articles