Last active
August 29, 2015 14:14
-
-
Save zapstar/107620036489ed2c0b06 to your computer and use it in GitHub Desktop.
Notes from the MongoDB introductory tutorial present at http://try.mongodb.org
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the MongoDB tutorial from http://try.mongodb.org | |
// Get started with `help` | |
// To try out an interactive tutorial type `tutorial` | |
// This shell is a (limited) javascript interpreter, so any commands you are familiar from javascript should work here. Try out: | |
var a = 5; | |
a * 10; | |
for(i=0; i<10; i++) { print('hello'); }; | |
// You can move onto the next step anytime by typing `next` | |
// MongoDB is a document database. This means that we store data as documents, which are similar to JavaScript objects. Here below are a few sample JS objects: | |
var a = {age: 25}; | |
var n = {name: 'Ed', languages: ['c', 'ruby', 'js']}; | |
var student = {name: 'Jim', scores: [75, 99, 87.2]}; | |
// Go ahead and create some documents, then enter 'next' | |
// Here's how you save a document to MongoDB: | |
db.scores.save({a: 99}); | |
// This says, "save the document '{a: 99}' to the 'scores' collection.". | |
// To confirm that it's been saved properly: | |
db.scores.find(); | |
// Try adding some documents to the scores collection: | |
for(i=0; i<20; i++) { db.scores.save({a: i, exam: 5}) }; | |
// Try that, then enter | |
db.scores.find(); | |
// to see if the save succeeded. Since the shell only displays 10 results at time, you'll need to enter the 'it' command to iterate over the rest. | |
// You've already tried a few queries, but let's make them more specific. | |
// Let's find all documents where a == 2: | |
db.scores.find({a: 2}); | |
// Or we could find all documents where a > 15: | |
db.scores.find({a: {'$gt': 15}}) | |
// `$gt` is one of many special query operators. Here are few others: | |
{a: {$lt: 5}} // Less Than | |
{a: {$gte: 10}} // Greater than or equal to | |
{a: {$ne: 'b'}} // Not Equal To | |
{a: {$in: ['a', 'b', 'c']}} // Exists in array | |
// Try out a few queries, before moving onto the next step. | |
// Now create a couple documents like these for updating: | |
db.users.insert({name: 'Johnny', languages: ['ruby', 'c']}); | |
db.users.insert({name: 'Sue', languages: ['scala', 'lisp']}); | |
// Confirm they were saved - with our favorite: | |
db.users.find() | |
// Update Johnny's name and languages: | |
db.users.update({name: 'Johnny'}, {name: 'Cash', languages: ['English']}); | |
// Use our favorite find query to inspect the resulting documents. Notice that the array update overwrote Johnny's languages! | |
// Play with some more updates, before continuing on. | |
// Update has the sometimes unexpected behavior of replacing the entire document. However, we can use update operators to only modify parts of our documents. | |
// Update Sue's languages without overwriting them: | |
db.users.update({name: 'Sue'}, { $addToSet: {languages: 'ruby'}}); | |
// Or we can add a new field to Cash | |
db.users.update({name: 'Cash'}, {'$set': {'age': 50} }); | |
// You can also push and pull items from arrays: | |
db.users.update({name: 'Sue'}, {'$push': {'languages': 'ruby'} }); | |
db.users.update({name: 'Sue'}, {'$pull': {'languages': 'scala'} }); | |
// Give these a try, check the results, and then enter 'next', | |
// To delete matching documents only, add a query selector to the remove method: | |
db.users.remove({name: 'Sue'}); | |
// To delete everything from a collection: | |
db.scores.remove({}); | |
// Congratulations! You've reached the end of this simple tutorial. | |
// Now take the next step with free MongoDB Online training at MongoDB University - university.mongodb.com! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment