// Check all the databases: show dbs // use new database (if doesn't exist). Adding collections will create the database automatically use hollywood // Check all the collections in the database hollywood show collections // Create collection actors and add two documents (records) in the hollywood database db.actors.insert( { actor: "Richard Gere", born:1949, movies: ['Pretty Woman', 'Runaway Bride', 'Chicago'] }); db.actors.insert( { actor: "Julia Roberts", born:1967, movies: ['Pretty Woman', 'Runaway Bride', 'Osage County'] }); db.actors.insert( { actor: "Meryl Streep", born:1948, movies: ['The Devil Wears Prada', 'Osage County', 'The Iron Lady', 'The Manchurian Candidate'] }); //find all documents in the collection actors: db.actors.find(); //find actors born after 1940: db.actors.find({born: {$gt: 1940}}); //find actors born in 1949: db.actors.find({born: 1949}); db.actors.find({born: {$eq: 1949}}); // find actors born in 1948, 1949 db.actors.find({born: {$in:[1949, 1948]}}); // This will remove the record for 'Richard Gere'!! db.actors.update({actor: 'Richard Gere'}, {gender: 'male'}); // Check what happened with actors collections after last update: db.actors.find() // Let's return back record for 'Richard Gere' db.actors.update( { gender: 'male'}, { actor: "Richard Gere", born:1949, movies: ['Pretty Woman', 'Runaway Bride', 'Chicago'] }); // Better way: db.actors.update( {_id: ObjectId("---put-record-id-here---")}, { actor: "Richard Gere", born:1949, movies: ['Pretty Woman', 'Runaway Bride', 'Chicago'] }); // This will not work!! You need to use ObjectId instead of ID db.actors.update( {_id: "---put-record-id-here---"}, { actor: "Richard Gere", born:1949, movies: ['Pretty Woman', 'Runaway Bride', 'Chicago'] }); // Fix year for Meryl - increase it by 1 db.actors.update({actor: 'Meryl Streep'}, {$inc: {born: 1}}); // Update just the name: db.actors.update({actor: 'Richard Gere'}, {$set: {actor: 'Richard Gere III'}}); // Add extra attribute: db.actors.update({actor: 'Meryl Streep'}, {$set: {gender: 'Female'}}); // Add extra movie to Meryl Streep: db.actors.update({actor: 'Meryl Streep'}, {$push: {movies: 'Out of Africe'}}); // List all the actors in the collection: db.actors.find({}, {actor: 1}); // Sort documents using two keys: born (ascending) and actor (descending and ascending) db.actors.find().sort({born: 1, actor: -1}); db.actors.find().sort({born: 1, actor: 1}); // We want to list now movies and actors played in each movie (as an array) // Add map and reduce functions: map = function() { for(var i in this.movies){ key = { movie: this.movies[i] }; value = { actors: [ this.actor ] }; emit(key, value); } } reduce = function(key, values) { actor_list = { actors: [] }; for(var i in values) { actor_list.actors = values[i].actors.concat(actor_list.actors); } return actor_list; } // Create new collection 'movies': db.actors.mapReduce(map, reduce, "movies"); // Check that you have now two collections: movies and actors: show collections // List all the movies in the just created movies collection: db.movies.find(); // Drop collection movies: db.movies.drop(); // Check that you have database hollywood selected db // Drop selected database hollywood: db.dropDatabase();