Skip to content
Browse files

Mastering the mondb shell fixes #3

  • Loading branch information...
1 parent 6f98c12 commit ad7b4a716699fa847f29df19a28508fbeed94ac7 @ericlemerdy committed Feb 26, 2012
Showing with 590 additions and 4 deletions.
  1. BIN 05_badge-shell.jpeg
  2. +313 −2 tutorial.html
  3. +277 −2 tutorial.txt
View
BIN 05_badge-shell.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
315 tutorial.html
@@ -240,7 +240,7 @@ <h1 class="title">MongoDB</h1>
<h1>Data breakdown structure</h1>
<ul>
-<li><p>Document &lt; Collection &lt; Database &lt; Runtime</p></li>
+<li><p>Document &lt; Collection &lt; Database &lt; Daemon</p></li>
<li><p>Document : JSON fragment</p>
<p>ex:</p>
<pre><code>{ &quot;mongo&quot; :
@@ -254,7 +254,7 @@ <h1 class="title">MongoDB</h1>
<pre><code>&gt; show dbs
local (empty)
</code></pre></li>
-<li><p>Runtime (daemon) : several Databases (physically stored in the dbpath)</p></li>
+<li><p>Daemon : several Databases (physically stored in the dbpath)</p></li>
</ul>
</div>
@@ -360,5 +360,316 @@ <h1 class="title">MongoDB</h1>
<li><p>More informations : <a href="http://www.10gen.com/presentations/mongosv-2011/schema-design-principles-and-practice">10gen - MongoDB Presentations - Schema Design Principles and Practice</a></p></li>
</ul>
</div>
+
+<div class="slide">
+
+<h1>Are you empty ?</h1>
+<ul>
+<li><p>Connect to a database</p>
+<pre><code>&gt; use test
+switched to db test
+</code></pre></li>
+<li><p>Find the collections of the database</p>
+<pre><code>&gt; db.getCollectionNames()
+[ ]
+</code></pre></li>
+<li><p>The database is empty !</p></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Create a collection</h1>
+<ul>
+<li><p>Let's create the cliparts collection.</p></li>
+<li><p>There is nothing to do as long you do not insert to a first document in it :</p>
+<pre><code>&gt; db.cliparts.findOne()
+null
+</code></pre></li>
+<li><p>Does a read has created the collection ?</p>
+<pre><code>&gt; db.getCollectionNames()
+[ ]
+</code></pre></li>
+<li><p>Obviously no</p></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Insert the document in the collection</h1>
+<pre><code>&gt; db.cliparts.save( clipart )
+</code></pre>
+<ul>
+<li><p>Does a write has created the collection ?</p>
+<pre><code>&gt; db.getCollectionNames()
+[ &quot;cliparts&quot;, &quot;system.indexes&quot; ]
+</code></pre></li>
+<li><p>Obviously yes</p></li>
+<li>Even 2 collections !
+<ul>
+<li>cliparts : the collection that contains our new document</li>
+<li>system.indexes : the index to allow mongo to be fast searching</li>
+</ul></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Counting</h1>
+<ul>
+<li><p>Counting elements</p>
+<pre><code>&gt; db.cliparts.count()
+1
+</code></pre></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Display collection content</h1>
+<ul>
+<li><p>Show me the content</p>
+<pre><code>&gt; db.cliparts.find()
+{ &quot;_id&quot; : ObjectId(&quot;4f32ed58a1d1c40cacf43637&quot;), &quot;title&quot; : &quot;Code Story logo&quot;, (...) &quot;image/png&quot; : &quot;http://openclipart.org/image/800px/svg_to_png/166309/Code_Story_Logo.png&quot; } }
+</code></pre></li>
+<li><p>Formatted</p>
+<pre><code>&gt; db.cliparts.findOne()
+{
+ &quot;_id&quot; : ObjectId(&quot;4f32ed58a1d1c40cacf43637&quot;),
+ &quot;title&quot; : &quot;Code Story logo&quot;,
+(...)
+ &quot;image/png&quot; : &quot;http://openclipart.org/image/800px/svg_to_png/166309/Code_Story_Logo.png&quot;
+ }
+}
+</code></pre></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>What's new ?</h1>
+<ul>
+<li><p>There is now an id in the object :</p>
+<pre><code>&gt; db.cliparts.find( { }, { &quot;_id&quot; : 1, &quot;title&quot; : 1 } )
+{ &quot;_id&quot; : ObjectId(&quot;4f32ed58a1d1c40cacf43637&quot;), &quot;title&quot; : &quot;Code Story logo&quot; }
+</code></pre></li>
+<li><p>Generated by the mongoDB instance</p></li>
+<li><p>Must be unique per collection (and per cluser)</p>
+<table>
+<col width="11%" />
+<col width="11%" />
+<col width="5%" />
+<col width="11%" />
+<thead>
+<tr class="header">
+<th align="left">0 1 2 3</th>
+<th align="left">4 5 6</th>
+<th align="left">7 8</th>
+<th align="left">9 10 11</th>
+</tr>
+</thead>
+<tbody>
+<tr class="odd">
+<td align="left">time</td>
+<td align="left">machine</td>
+<td align="left">pid</td>
+<td align="left">inc</td>
+</tr>
+</tbody>
+</table></li>
+<li><p>Unix time + md5 machine + process id + increment value</p></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Insert another document</h1>
+<ul>
+<li><p>Create a second variable</p>
+<pre><code>&gt; clipart2 = {
+ &quot;title&quot; : &quot;PC LAPTOP NOTEBOOK&quot;,
+ &quot;uploader&quot; : { &quot;name&quot; : &quot;Keistutis&quot;, &quot;href&quot; : &quot;http://openclipart.org/user-detail/Keistutis&quot; },
+ &quot;drawn by&quot; : &quot;Kostas Šliažas / Keistutis&quot;, &quot;created&quot; : ISODate(&quot;2012-02-23T09:28:36.700Z&quot;),
+ &quot;description&quot; : null, &quot;tags&quot; : [ &quot;PC&quot;, &quot;LAPTOP&quot;, &quot;NOTEBOOK&quot; ],
+ &quot;viewed by&quot; : 173, &quot;image&quot; : {
+ &quot;image/svg+xml&quot; : &quot;http://openclipart.org/people/Keistutis/PC.svg&quot;,
+ &quot;image/png&quot; : &quot;http://openclipart.org/image/800px/svg_to_png/168489/PC.png&quot;
+ }
+}
+</code></pre></li>
+<li><p>Insert it twice</p>
+<pre><code>&gt; db.cliparts.save( clipart2 )
+&gt; delete clipart2._id # Otherwise it will be saved in the same document.
+true
+&gt; db.cliparts.save( clipart2 )
+</code></pre></li>
+<li><p>Verify</p>
+<pre><code>&gt; db.cliparts.count()
+3
+</code></pre></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Edit a document</h1>
+<ul>
+<li><p>Extract the first uploader :</p>
+<pre><code>&gt; var ericUploader = db.cliparts.findOne( { &quot;uploader.name&quot; : /eric/ } ).uploader
+&gt; ericUploader
+{
+ &quot;name&quot; : &quot;ericlemerdy&quot;,
+ &quot;href&quot; : &quot;http://openclipart.org/user-detail/ericlemerdy&quot;
+}
+</code></pre></li>
+<li><p>Set it in the second document :</p>
+<pre><code>&gt; db.cliparts.findAndModify( {
+ query : { &quot;title&quot; : &quot;PC LAPTOP NOTEBOOK&quot; }, // Query selector.
+ sort : {}, // Sort in case several docs are matched.
+ // remove : true, // To delete the document.
+ update : { $set : { &quot;uploader&quot; : ericUploader } }, // Modifier object.
+ new : false, // True to get the newly created document.
+ // fields : {}, // Retrieve only a sub-set of the document fields.
+ upsert : false // True to create object if it does not exists.
+} )
+{
+ &quot;_id&quot; : ObjectId(&quot;4f4a3df18d08530d9516afbe&quot;),
+ &quot;title&quot; : &quot;PC LAPTOP NOTEBOOK&quot;,
+ &quot;uploader&quot; : {
+ &quot;name&quot; : &quot;Keistutis&quot;,
+ &quot;href&quot; : &quot;http://openclipart.org/user-detail/Keistutis&quot;
+ },
+ (...)
+}
+</code></pre></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Verify document</h1>
+<ul>
+<li><p>Find it :</p>
+<pre><code>&gt; db.cliparts.findOne( { &quot;title&quot; : /PC/, &quot;uploader.name&quot; : /eric/ } )
+{
+ &quot;_id&quot; : ObjectId(&quot;4f4a3df18d08530d9516afbe&quot;),
+ &quot;created&quot; : ISODate(&quot;2012-02-23T09:28:36.700Z&quot;),
+ &quot;description&quot; : null,
+ &quot;drawn by&quot; : &quot;Kostas Šliažas / Keistutis&quot;,
+ &quot;image&quot; : {
+ &quot;image/svg+xml&quot; : &quot;http://openclipart.org/people/Keistutis/PC.svg&quot;,
+ &quot;image/png&quot; : &quot;http://openclipart.org/image/800px/svg_to_png/168489/PC.png&quot;
+ },
+ &quot;tags&quot; : [
+ &quot;PC&quot;,
+ &quot;LAPTOP&quot;,
+ &quot;NOTEBOOK&quot;
+ ],
+ &quot;title&quot; : &quot;PC LAPTOP NOTEBOOK&quot;,
+ &quot;uploader&quot; : {
+ &quot;name&quot; : &quot;ericlemerdy&quot;,
+ &quot;href&quot; : &quot;http://openclipart.org/user-detail/ericlemerdy&quot;
+ },
+ &quot;viewed by&quot; : 173
+}
+</code></pre></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Distinct values</h1>
+<ul>
+<li><p>What are the distinct title of the cliparts collection ?</p>
+<pre><code>&gt; db.cliparts.distinct(&quot;title&quot;)
+[ &quot;Code Story logo&quot;, &quot;PC LAPTOP NOTEBOOK&quot; ]
+</code></pre></li>
+<li><p>What are the distinct tags of the cliparts collection ?</p>
+<pre><code>&gt; db.cliparts.distinct(&quot;tags&quot;)
+[ &quot;code&quot;, &quot;computer&quot;, &quot;developpers&quot;, &quot;logo&quot;, &quot;people&quot;, &quot;public domain&quot;, &quot;LAPTOP&quot;, &quot;NOTEBOOK&quot;, &quot;PC&quot; ]
+</code></pre></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Explain query</h1>
+<pre><code> &gt; db.cliparts.find().explain()
+ {
+ &quot;cursor&quot; : &quot;BasicCursor&quot;,
+ &quot;nscanned&quot; : 3,
+ &quot;nscannedObjects&quot; : 3,
+ &quot;n&quot; : 3,
+ &quot;millis&quot; : 0,
+ &quot;nYields&quot; : 0,
+ &quot;nChunkSkips&quot; : 0,
+ &quot;isMultiKey&quot; : false,
+ &quot;indexOnly&quot; : false,
+ &quot;indexBounds&quot; : {
+
+ }
+ }
+</code></pre>
+<ul>
+<li><p>Traversing the entire collection</p>
+<pre><code>&gt; db.cliparts.find( { &quot;uploader.name&quot;: /eric/ } ).explain()
+{
+ &quot;cursor&quot; : &quot;BasicCursor&quot;,
+ &quot;nscanned&quot; : 3,
+ &quot;nscannedObjects&quot; : 3,
+ &quot;n&quot; : 2,
+ &quot;millis&quot; : 0,
+ &quot;nYields&quot; : 0,
+ &quot;nChunkSkips&quot; : 0,
+ &quot;isMultiKey&quot; : false,
+ &quot;indexOnly&quot; : false,
+ &quot;indexBounds&quot; : {
+
+ }
+}
+</code></pre></li>
+<li><p>Same traversing but only 2 objects returned.</p></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Create an index</h1>
+<ul>
+<li><p>Create a composite index on uploader name:</p>
+<pre><code>&gt; db.cliparts.createIndex( { &quot;uploader.name&quot; : 1 } )
+</code></pre></li>
+<li><p>Visualize effect on search</p>
+<pre><code>&gt; db.cliparts.find( { &quot;uploader.name&quot;: /eric/ } ).explain()
+{
+ &quot;cursor&quot; : &quot;BtreeCursor uploader.name_1 multi&quot;,
+ &quot;nscanned&quot; : 3,
+ &quot;nscannedObjects&quot; : 2,
+ &quot;n&quot; : 2,
+ &quot;millis&quot; : 0,
+ &quot;nYields&quot; : 0,
+ &quot;nChunkSkips&quot; : 0,
+ &quot;isMultiKey&quot; : false,
+ &quot;indexOnly&quot; : false,
+ &quot;indexBounds&quot; : {
+ &quot;uploader.name&quot; : [
+ [ &quot;&quot;, { } ],
+ [ /eric/, /eric/ ]
+ ]
+ }
+}
+</code></pre></li>
+</ul>
+</div>
+
+<div class="slide">
+
+<h1>Achievement 3 : Master shell</h1>
+<ul>
+<li><p>Second achievement :</p>
+<p><img src="05_badge-shell.jpeg" alt="Be a shell master or the mastershell" /> Be a master shell !</p></li>
+<li><p>Let's move on to java !</p></li>
+</ul>
+</div>
</body>
</html>
View
279 tutorial.txt
@@ -200,7 +200,7 @@ First Achievement : MongoDB startup
Data breakdown structure
========================
- - Document < Collection < Database < Runtime
+ - Document < Collection < Database < Daemon
- Document : JSON fragment
@@ -219,7 +219,7 @@ Data breakdown structure
> show dbs
local (empty)
- - Runtime (daemon) : several Databases (physically stored in the dbpath)
+ - Daemon : several Databases (physically stored in the dbpath)
JSON
====
@@ -322,3 +322,278 @@ Achievement 2 : Document schema design principles
![NoSQL document database != relationnal](04_badge-nosql.jpeg) Document oriented schema design is different from relational !
- More informations : [10gen - MongoDB Presentations - Schema Design Principles and Practice](http://www.10gen.com/presentations/mongosv-2011/schema-design-principles-and-practice)
+
+Are you empty ?
+===============
+
+ - Connect to a database
+
+ > use test
+ switched to db test
+
+ - Find the collections of the database
+
+ > db.getCollectionNames()
+ [ ]
+
+ - The database is empty !
+
+Create a collection
+===================
+
+ - Let's create the cliparts collection.
+
+ - There is nothing to do as long you do not insert to a first document in it :
+
+ > db.cliparts.findOne()
+ null
+
+ - Does a read has created the collection ?
+
+ > db.getCollectionNames()
+ [ ]
+
+ - Obviously no
+
+Insert the document in the collection
+=====================================
+
+ > db.cliparts.save( clipart )
+
+ - Does a write has created the collection ?
+
+ > db.getCollectionNames()
+ [ "cliparts", "system.indexes" ]
+
+ - Obviously yes
+
+ - Even 2 collections !
+ - cliparts : the collection that contains our new document
+ - system.indexes : the index to allow mongo to be fast searching
+
+Counting
+========
+
+ - Counting elements
+
+ > db.cliparts.count()
+ 1
+
+Display collection content
+==========================
+
+ - Show me the content
+
+ > db.cliparts.find()
+ { "_id" : ObjectId("4f32ed58a1d1c40cacf43637"), "title" : "Code Story logo", (...) "image/png" : "http://openclipart.org/image/800px/svg_to_png/166309/Code_Story_Logo.png" } }
+
+ - Formatted
+
+ > db.cliparts.findOne()
+ {
+ "_id" : ObjectId("4f32ed58a1d1c40cacf43637"),
+ "title" : "Code Story logo",
+ (...)
+ "image/png" : "http://openclipart.org/image/800px/svg_to_png/166309/Code_Story_Logo.png"
+ }
+ }
+
+What's new ?
+============
+
+ - There is now an id in the object :
+
+ > db.cliparts.find( { }, { "_id" : 1, "title" : 1 } )
+ { "_id" : ObjectId("4f32ed58a1d1c40cacf43637"), "title" : "Code Story logo" }
+
+ - Generated by the mongoDB instance
+
+ - Must be unique per collection (and per cluser)
+
+ +-------+-------+---+-------+
+ |0 1 2 3|4 5 6 |7 8|9 10 11|
+ +=======+=======+===+=======+
+ |time |machine|pid|inc |
+ +-------+-------+---+-------+
+
+ - Unix time + md5 machine + process id + increment value
+
+Insert another document
+=======================
+
+ - Create a second variable
+
+ > clipart2 = {
+ "title" : "PC LAPTOP NOTEBOOK",
+ "uploader" : { "name" : "Keistutis", "href" : "http://openclipart.org/user-detail/Keistutis" },
+ "drawn by" : "Kostas Šliažas / Keistutis", "created" : ISODate("2012-02-23T09:28:36.700Z"),
+ "description" : null, "tags" : [ "PC", "LAPTOP", "NOTEBOOK" ],
+ "viewed by" : 173, "image" : {
+ "image/svg+xml" : "http://openclipart.org/people/Keistutis/PC.svg",
+ "image/png" : "http://openclipart.org/image/800px/svg_to_png/168489/PC.png"
+ }
+ }
+
+ - Insert it twice
+
+ > db.cliparts.save( clipart2 )
+ > delete clipart2._id # Otherwise it will be saved in the same document.
+ true
+ > db.cliparts.save( clipart2 )
+
+ - Verify
+
+ > db.cliparts.count()
+ 3
+
+Edit a document
+===============
+
+ - Extract the first uploader :
+
+ > var ericUploader = db.cliparts.findOne( { "uploader.name" : /eric/ } ).uploader
+ > ericUploader
+ {
+ "name" : "ericlemerdy",
+ "href" : "http://openclipart.org/user-detail/ericlemerdy"
+ }
+
+ - Set it in the second document :
+
+ > db.cliparts.findAndModify( {
+ query : { "title" : "PC LAPTOP NOTEBOOK" }, // Query selector.
+ sort : {}, // Sort in case several docs are matched.
+ // remove : true, // To delete the document.
+ update : { $set : { "uploader" : ericUploader } }, // Modifier object.
+ new : false, // True to get the newly created document.
+ // fields : {}, // Retrieve only a sub-set of the document fields.
+ upsert : false // True to create object if it does not exists.
+ } )
+ {
+ "_id" : ObjectId("4f4a3df18d08530d9516afbe"),
+ "title" : "PC LAPTOP NOTEBOOK",
+ "uploader" : {
+ "name" : "Keistutis",
+ "href" : "http://openclipart.org/user-detail/Keistutis"
+ },
+ (...)
+ }
+
+Verify document
+===============
+
+ - Find it :
+
+ > db.cliparts.findOne( { "title" : /PC/, "uploader.name" : /eric/ } )
+ {
+ "_id" : ObjectId("4f4a3df18d08530d9516afbe"),
+ "created" : ISODate("2012-02-23T09:28:36.700Z"),
+ "description" : null,
+ "drawn by" : "Kostas Šliažas / Keistutis",
+ "image" : {
+ "image/svg+xml" : "http://openclipart.org/people/Keistutis/PC.svg",
+ "image/png" : "http://openclipart.org/image/800px/svg_to_png/168489/PC.png"
+ },
+ "tags" : [
+ "PC",
+ "LAPTOP",
+ "NOTEBOOK"
+ ],
+ "title" : "PC LAPTOP NOTEBOOK",
+ "uploader" : {
+ "name" : "ericlemerdy",
+ "href" : "http://openclipart.org/user-detail/ericlemerdy"
+ },
+ "viewed by" : 173
+ }
+
+Distinct values
+===============
+
+ - What are the distinct title of the cliparts collection ?
+
+ > db.cliparts.distinct("title")
+ [ "Code Story logo", "PC LAPTOP NOTEBOOK" ]
+
+ - What are the distinct tags of the cliparts collection ?
+
+ > db.cliparts.distinct("tags")
+ [ "code", "computer", "developpers", "logo", "people", "public domain", "LAPTOP", "NOTEBOOK", "PC" ]
+
+Explain query
+=============
+
+ > db.cliparts.find().explain()
+ {
+ "cursor" : "BasicCursor",
+ "nscanned" : 3,
+ "nscannedObjects" : 3,
+ "n" : 3,
+ "millis" : 0,
+ "nYields" : 0,
+ "nChunkSkips" : 0,
+ "isMultiKey" : false,
+ "indexOnly" : false,
+ "indexBounds" : {
+
+ }
+ }
+
+ - Traversing the entire collection
+
+ > db.cliparts.find( { "uploader.name": /eric/ } ).explain()
+ {
+ "cursor" : "BasicCursor",
+ "nscanned" : 3,
+ "nscannedObjects" : 3,
+ "n" : 2,
+ "millis" : 0,
+ "nYields" : 0,
+ "nChunkSkips" : 0,
+ "isMultiKey" : false,
+ "indexOnly" : false,
+ "indexBounds" : {
+
+ }
+ }
+
+ - Same traversing but only 2 objects returned.
+
+Create an index
+===============
+
+ - Create a composite index on uploader name:
+
+ > db.cliparts.createIndex( { "uploader.name" : 1 } )
+
+ - Visualize effect on search
+
+ > db.cliparts.find( { "uploader.name": /eric/ } ).explain()
+ {
+ "cursor" : "BtreeCursor uploader.name_1 multi",
+ "nscanned" : 3,
+ "nscannedObjects" : 2,
+ "n" : 2,
+ "millis" : 0,
+ "nYields" : 0,
+ "nChunkSkips" : 0,
+ "isMultiKey" : false,
+ "indexOnly" : false,
+ "indexBounds" : {
+ "uploader.name" : [
+ [ "", { } ],
+ [ /eric/, /eric/ ]
+ ]
+ }
+ }
+
+Achievement 3 : Master shell
+=================================================
+
+ - Second achievement :
+
+ ![Be a shell master or the mastershell](05_badge-shell.jpeg) Be a master shell !
+
+ - Let's move on to java !
+
+

0 comments on commit ad7b4a7

Please sign in to comment.
Something went wrong with that request. Please try again.