Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added tutorial

  • Loading branch information...
commit 7c82f11bfae7c0febd1c81499c0eb4ca489dc80f 1 parent a11d723
@banker banker authored
View
4 mongulator.rb
@@ -24,7 +24,9 @@ def scoped_collection(name)
post '/insert' do
coll = scoped_collection(params['name'])
- coll.insert(JSON.parse(params['doc']))
+ if coll.count < 200
+ coll.insert(JSON.parse(params['doc']))
+ end
end
post '/update' do
View
2  public/css/mongo.css
@@ -1,11 +1,13 @@
body {
font-family: Monaco, serif;
font-size: 20px;
+ background-color: #f3f4eb;
}
#header {
width: 960px;
margin:50px auto 50px auto;
+ color: #402817;
font-family: 'Baskerville','Palatino Linotype','Times New Roman',serif;
}
View
5 public/index.html
@@ -7,14 +7,15 @@
</head>
<body>
<div id="header">
- <h1>Try MongoDB:</h1>
- <h2>A MongoDB Browser Shell</h2>
+ <h2>A Tiny MongoDB Browser Shell (mini tutorial included)</h2>
+ <h2>Just enough to scratch the surface</h2>
</div>
<div id="terminal">
<p class="response">MongoDB browser shell version: 0.1.0</p>
<p>connecting to random database</p>
<p>type "help" for help</p>
+ <p>type "tutorial" to start the tutorial</p>
</div>
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
View
6 public/js/constants.js
@@ -27,8 +27,12 @@ var PTAG = function(str) {
return "<pre>" + str + "</pre>";
}
+var BR = function() {
+ return "<br/>";
+}
+
var JavascriptKeywords = ['abstract', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'double', 'else', 'enum', 'export', 'extends', 'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'long', 'native', 'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'var', 'void', 'volatile', 'while', 'with', 'alert', 'date', 'eval'];
var JavascriptClassNames = ['Array', 'String', 'Object']
-var MongoKeywords = ['help', 'it', 'show'];
+var MongoKeywords = ['use', 'help', 'it', 'show', 'tutorial', 'next', 'back', 't0', 't1', 't2', 't3', 't4', 't5', 't6', 't7', 't8', 't9', 't10'];
View
198 public/js/mongo.js
@@ -123,11 +123,13 @@ var MongoHandler = function() {
this._rawCommand = "";
this._commandStack = 0;
this._currentDB = "test";
+ this._tutorialPtr = 0;
+ this._tutorialMax = 10;
this._mongo = {};
this._mongo.test = [];
this.db = this._mongo.test;
- this._dbPtr = 'test';
+ this._dbPtr = 'test';
this.collections = [];
};
@@ -176,9 +178,10 @@ MongoHandler.prototype = {
_evaluator: function(tokens) {
this._currentCommand += " " + this._massageTokens(tokens);
if(this._shouldEvaluateCommand(tokens)) {
- db = this.db;
+ db = this.db;
+ print = this.print;
- // So this is the heart of the REPL.
+ // So this eval statement is the heart of the REPL.
var result = eval(this._currentCommand.trim());
if(result === undefined) {
throw('result is undefined');
@@ -262,7 +265,7 @@ MongoHandler.prototype = {
// print output to the screen, e.g., in a loop
// TODO: remove dependency here
print: function() {
- $('.readline.active').parent().append('<p>' + arguments[0] + '</p>');
+ $('.readline.active').parent().append('<p>' + arguments[0] + '</p>');
return '';
},
@@ -278,14 +281,165 @@ MongoHandler.prototype = {
// help command
_help: function() {
return PTAG('HELP') +
- PTAG('show collections show collections in current database') +
- PTAG('db.help() help on DB methods') +
+ PTAG('Note: Only a subset of MongoDB\'s features are provided here.') +
+ PTAG('For everything else, download and install at mongodb.org.') +
PTAG('db.foo.help() help on collection methods') +
PTAG('db.foo.find() list objects in collection foo') +
PTAG('db.foo.save({a: 1}) save a document to collection foo') +
PTAG('db.foo.update({a: 1}, {a: 2}) update document where a == 1') +
PTAG('db.foo.find({a: 1}) list objects in foo where a == 1') +
- PTAG('it result of the last line evaluated; use to further iterate');
+ PTAG('it use to further iterate over a cursor');
+
+ },
+
+ _use: function() {
+ return "Sorry, you can't change the database you're using."
+ },
+
+ _tutorial: function() {
+ this._tutorialPtr = 0;
+ return PTAG("This is a self-guided tutorial on MongoDB and the MongoDB shell.") +
+ PTAG("The tutorial is simple, more or less a few basic commands to try.") +
+ PTAG("To go directly to any part tutorial, enter one of the commands t0, t1, t2...t10") +
+ PTAG("Otherwise, use 'next' and 'back'. Start by typing 'next' and pressing enter.");
+ },
+
+ // go to the next step in the tutorial.
+ _next: function() {
+ if(this._tutorialPtr < this._tutorialMax) {
+ return this['_t' + (this._tutorialPtr + 1)]();
+ }
+ else {
+ return "You've reached the end of the tutorial. To go to the beginning, type 'tutorial'";
+ }
+ },
+
+ // go to the previous step in the tutorial.
+ _back: function() {
+ if(this._tutorialPtr > 1) {
+ return this['_t' + (this._tutorialPtr - 1)]();
+ }
+ else {
+ return this._tutorial();
+ }
+ },
+
+ _t1: function() {
+ this._tutorialPtr = 1;
+ return PTAG('1. JavaScript Shell') +
+ PTAG('The first thing to notice is that the MongoDB shell is JavaScript-based.') +
+ PTAG('So you can do things like:') +
+ PTAG(' a = 5') +
+ PTAG(' a * 10') +
+ PTAG(" for(i=0; i<10; i++) { print('hello'); }") +
+ PTAG("Try a few JS commands; when you're ready to move on, enter 'next'");
+
+ },
+
+ _t2: function() {
+ this._tutorialPtr = 2;
+ return PTAG("2. Documents") +
+ PTAG("MongoDB is a document database. This means that we store data as documents,") +
+ PTAG("which are simiar to JavaScript objects. Here below are a few sample JS objects:") +
+ PTAG(' var a = {age: 25}') +
+ PTAG(" var n = {name: 'Ed', languages: ['c', 'ruby', 'js']}") +
+ PTAG(" var student = {name: 'Jim', scores: [75, 99, 87.2]}") +
+ PTAG("Create some documents, then enter 'next'");
+ },
+
+
+ _t3: function() {
+ this._tutorialPtr = 3;
+ return PTAG('3. Saving') +
+ PTAG("Here's how you save a document to MongoDB:") +
+ PTAG(" db.scores.save({a: 99})") +
+ BR() +
+ PTAG("This says, \"save the document '{a: 99}' to the 'scores' collection.\"") +
+ PTAG("Go ahead and try it. Then, to see if the document was saved, try") +
+ PTAG(" db.scores.find()") +
+ PTAG("Once you've tried this, type 'next'.") +
+ BR();
+ },
+
+ _t4: function() {
+ this._tutorialPtr = 4;
+ return PTAG('4. Saving and Querying') +
+ PTAG("Try adding some documents to the scores collection:") +
+ PTAG(" for(i=0; i<10; i++) { db.scores.save({a: i, exam: 5})") +
+ BR() +
+ PTAG("Try that, then enter") +
+ PTAG(" db.scores.find()") +
+ PTAG("to see the save succeeded. Since the shell only displays 10 results at time,") +
+ PTAG("you'll need to enter the 'it' command to iterate over the rest.") +
+ BR() +
+ PTAG("(enter 'next' when you're ready)");
+ },
+
+ _t5: function() {
+ this._tutorialPtr = 5;
+ return PTAG('5. Basic Queries') +
+ PTAG("You've already tried a few queries, but let's make them more specific.") +
+ PTAG("How about finding all documents where a == 2:") +
+ PTAG(" db.scores.find({a: 2})") +
+ BR() +
+ PTAG("Or what about documents where a > 15?") +
+ PTAG(" db.scores.find({a: {'$gt': 15}})") +
+ BR();
+ },
+
+ _t6: function() {
+ this._tutorialPtr = 6;
+ return PTAG('6. Query Operators') +
+ PTAG("Query Operators:") +
+ PTAG("$gt is one of many special query operators. Here are few others:") +
+ PTAG(" $lt - '<', $lte - '<=', ") +
+ PTAG(" $gte - '>=', $ne - '!='") +
+ PTAG(" $in - 'is in array', $nin - '! in array'") +
+ BR() +
+ PTAG("db.scores.find({a: {'$in': [2, 3, 4]}})") +
+ PTAG("db.scores.find({a: {'$gte': 2, '$lte': 4}})") +
+ PTAG("Try creating some queries, then move on to t7") +
+ BR();
+ },
+
+ _t7: function() {
+ this._tutorialPtr = 7;
+ return PTAG('7. Updates') +
+ PTAG("Now create a couple documents like these for updating:") +
+ PTAG(" db.users.save({name: 'Johnny', languages: ['ruby', 'c']})") +
+ PTAG(" db.users.save({name: 'Sue', languages: ['scala', 'lisp']})") +
+ PTAG("Make sure they were saved by called db.users.find()") +
+ PTAG("Update the first document like so:") +
+ PTAG(" db.users.update({name: 'Johnny'}, {name: 'Cash', languages: ['english']})");
+ },
+
+ _t8: function() {
+ this._tutorialPtr = 8;
+ return PTAG('8. Update Operators') +
+ PTAG("The previous update replaced the entire document, but MongoDB also") +
+ PTAG("supports partial updates to documents. For example, you can set a value:") +
+ PTAG(" db.users.update({name: 'Cash'}, {'$set': {'age': 50} })") +
+ PTAG("You can also push and pop (pull in MongoDB) items from arrays:") +
+ PTAG(" db.users.update({name: 'Sue'}, {'$pull': {'languages': 'scala'} })") +
+ PTAG(" db.users.update({name: 'Sue'}, {'$push': {'languages': 'ruby'} })") +
+ PTAG("Give these a try, check the results, and then enter 'next'.)");
+ },
+
+ _t9: function() {
+ this._tutorialPtr = 9;
+ return PTAG('9. Deleting data') +
+ PTAG("To delete everything from a collection:") +
+ PTAG(" db.scores.remove()") +
+ PTAG("To delete matching documents only, add a query selector to the remove method:") +
+ PTAG(" db.users.remove({name: 'Sue'})");
+ },
+
+ _t10: function() {
+ this._tutorialPtr = 10;
+ return PTAG('10. Now go download it!') +
+ PTAG("There's a lot more to MongoDB than what's presented in this tutorial.") +
+ PTAG("Best thing is to go to the <a href='http://www.mongodb.org/display/DOCS/Downloads'>downloads page</a> or to <a href='http://mongodb.org'>mongodb.org</a> to check out the docs.") +
+ PTAG("(You can also keep fiddling around here, but you'll be a bit limited.)");
},
_iterate: function() {
@@ -310,6 +464,36 @@ MongoHandler.prototype = {
switch(tokens[0].value.toLowerCase()) {
case 'help':
return this._help;
+ case 'use':
+ return this._use;
+ case 'tutorial':
+ return this._tutorial;
+ case 'next':
+ return this._next;
+ case 'back':
+ return this._back;
+ case 't0':
+ return this._ideas;
+ case 't1':
+ return this._t1;
+ case 't2':
+ return this._t2;
+ case 't3':
+ return this._t3;
+ case 't4':
+ return this._t4;
+ case 't5':
+ return this._t5;
+ case 't6':
+ return this._t6;
+ case 't7':
+ return this._t7;
+ case 't8':
+ return this._t8;
+ case 't9':
+ return this._t9;
+ case 't10':
+ return this._t10;
case 'show':
if(tokens[1].value.toLowerCase() == 'collections') {
return this._showCollections;
Please sign in to comment.
Something went wrong with that request. Please try again.