Tuesday, February 3, 2015

MongoDB

The last few days I had a look on MongoDB (using windows)...

MongoDB was developed by a company called 10gen and was open-sourced in 2009. The reason they started building a database was simply the lack of usable NoSql databases for their own service. 2013 they renamed their company to MongoDB Inc. to express their new focus. A key player here is Dwight Merriman.

MongoDB itself is a C++ open source NoSQL schemaless (but can be used with a schema) document store (IaaS) storing BSON (Binary JSON) documents with JavaScript as procedure language and good scaling behavior (CAP theorem: partial consistency).

Installation process is well documented and worked for me without any troubles. Best thing is that it can be skipped using mongoDB portable (e.g.: ZWAMP is a portable windows web-stack with mongo). The only thing to do is to create a config file or set --dbpath to a folder with the data and start mongod (daemon). Another nice feature is the ootb http-interface (can be enabled by argument) and the --rest switch which allows basic data querying.

Tools:

  • php MongoDB admin
  • MMS: Mongo Management Service
  • Mongo Shell (ootb)
  • RoboMongo
  • MongoVUE
  • Query Translator ( http://www.querymongo.com/ )
    e.g.: translates 'select * from x where _id <= "3"' to '
    db['x'].find({"_id": { "$lte" : "3" }});'
The following code (php) shows my first tries with mongoDB (I used dBug http://dbug.ospinto.com/ http://sourceforge.net/projects/php-dbug/ for visualization)



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php 
 header('Pragma: no-cache');
 include_once('dbug\dbug.php'); // its open source and a single file to copy http://dbug.ospinto.com/
 if (!class_exists('Mongo')) die("Mongo support required. Install mongo pecl extension with 'pecl install mongo; echo \"extension=mongo.so\" >> php.ini'");

 // returns a connection object to work with
 function Connect()
 {
  try
  {
     return new Mongo('mongodb://localhost:27017', array('connect' => true));
  }
  catch (MongoConnectionException $ex)
  {
     error_log($ex->getMessage());
     die("Failed to connect to MongoDB");
  }
 }

 function ShowDBs()
 {
  $dbs = Connect()->listDBs();
  new dBug($dbs);
  echo "<hr />";
 }

 function CreateDB($name)
 {
  $mongo = Connect();
  $mongo->selectDB($name)->createCollection('__tmp_collection_');
  $mongo->selectDB($name)->dropCollection('__tmp_collection_');
   }
   
 function DropDB($name)
 {
  Connect()
   ->selectDB($name)
   ->drop ();
 }

 function CreateCollection($db, $col)
 {
  Connect()
   ->selectDB($db)
   ->createCollection($col);
 }

 function AddDocument($db, $col, $doc)
 {
  Connect()
   ->selectDB($db)
   ->selectCollection($col)
   ->save($doc);
 }

 function ShowDocuments($db, $col)
 {
  $cur = Connect()
   ->selectDB($db)
   ->selectCollection($col)
   ->find();
  
  foreach($cur as $data) 
  {
   echo "\n<div style='float:left;margin-right:10px'>\n";
   new dBug($data);
   echo "\n</div>\n";
  }
 }

 function CreateSPToAddNumbers($db,$col)
 {
  Connect()
   ->selectDB($db)
   ->selectCollection('system.js')
   ->save(array(
    '_id'   =>                        'addNumbers',
    'value' => new MongoCode('function addNumbers(x, y) { return x + y; }')
   ));
   
 }

 function CallSPToAddNumbers($db, $col)
 {
  echo "<div style='clear:left'/><div style='padding-top:40px'>Calculation: 30 + 12 = ";
  var_dump(Connect()->selectDB($db)->execute(
    'function(x, y) { return addNumbers(x,y); }', 
    array(30, 12)));
  echo "</div>";
 }

?>

<html>
<head><title>mongo test</title></head>
<body>
<?php

 //ShowDBs();
 DropDB('CC');

 //ShowDBs();
 CreateDB('CC');
 // die();
 
 //ShowDBs();
 CreateCollection('CC','x');
 // die();
 
 AddDocument('CC','x', array("_id" => "1", "name" => "John",     "details" => array("born" => "1986", "status" => "tired")));
 AddDocument('CC','x', array("_id" => "2", "name" => "Lenny",    "details" => array("born" => "1981", "status" => "tired")));
 AddDocument('CC','x', array("_id" => "3", "name" => "Frank",    "details" => array(                  "status" => "tired")));
 AddDocument('CC','x', array("_id" => "4", "name" => "Ryan",     "details" => array("born" => "1988", "status" => "tired")));
 AddDocument('CC','x', array("_id" => "5", "name" => "Will",     "details" => array("born" => "1990", "status" => "tired")));

 ShowDocuments('CC','x');

 CreateSPToAddNumbers('CC','x');
 CallSPToAddNumbers  ('CC','x');
?>
</body>
</html>

... the code drops and then creates a db 'CC' (cloud computing) with a collection x with 5 documents then shows the document and the result of an example function.

kr, Daniel

No comments: