+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Drop persons table and recreate it +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ db.persons.drop() db.persons.insert ( { name: {first: 'Harish', last: 'Chandra'}, gender: 'M', yearOfBirth: 1962, livesIn: 'Mumbai', countriesVisited: ['India', 'Singapore', 'Thailand', 'United Kingdom', 'Spain', 'Denmark', 'United States of America'], languages: [ {name: 'Hindi', proficiency: 'Fluent'}, {name: 'English', proficiency: 'Fluent'}, {name: 'Sanskrit', proficiency: 'Intermediate'} ] } ) db.persons.insert ( { name: {first: 'Zoa', last: 'Hendrik'}, gender: 'F', yearOfBirth: 1988, livesIn: 'Barcelona', countriesVisited: ['Spain', 'Denmark', 'France'], languages: [ {name: 'Spanish', proficiency: 'Fluent'}, {name: 'Catalan', proficiency: 'Fluent'}, {name: 'English', proficiency: 'Intermediate'} ], married: 'No' } ) db.persons.insert ( { name: {first: 'Narayan', last: 'Sundaram'}, gender: 'M', yearOfBirth: 1960, livesIn: 'Jaipur', counriesVisited: ['India', 'United States of America', 'Netherlands', 'China'], languages: [ {name: 'Tamil', proficiency: 'Fluent'}, {name: 'English', proficiency: 'Fluent'}, {name: 'Hindi', proficiency: 'Intermediate'}, {name: 'Chinese', proficiency: 'Intermediate'} ], married: 'Yes' } ) db.persons.insert ( { name: {first: 'Shailesh', last: 'mark'}, gender: 'M', yearOfBirth: 1962, livesIn: 'Singapore', countriesVisited: ['India', 'Singapore', 'China', 'Taiwan', 'United States of America', 'Sri Lanka', 'Mayalsia', 'Australia'], languages: [ {name: 'Hindi', proficiency: 'Fluent'}, {name: 'English', proficiency: 'Fluent'} ] } ) db.persons.insert ( { name: {first: 'Mark', last: 'Smith'}, gender: 'M', livesIn: 'Chicago', countriesVisited: ['United States of America', 'Canada', 'France', 'Spain', 'India', 'Australia'], languages: [ {name: 'English', proficiency: 'Fluent'} ] } ) db.persons.insert ( { name: {first: 'Rita', last: 'Chauhan'}, gender: 'F', yearOfBirth: 1989, livesIn: 'Mumbai', countriesVisited: ['India'], languages: [ {name: 'Hindi', proficiency: 'Fluent'}, {name: 'English', proficiency: 'Fluent'} ], married: 'No' } ) db.persons.insert ( { name: {first: 'Liza', last: 'Bordelon'}, gender: 'F', yearOfBirth: 1971, livesIn: 'Minneapolis', countriesVisited: ['United States of America', 'Canada'], languages: [ {name: 'English', proficiency: 'Fluent'}, {name: 'French', proficiency: 'Fluent'}, {name: 'German', proficiency: 'Intermediate'}, {name: 'Greek', proficiency: 'Intermediate'}, {name: 'Latin', proficiency: 'Intermediate'}, {name: 'Sanskrit', proficiency: 'Beginner'} ] } ) db.persons.insert ( { name: {first: 'Jenny', last: 'Jones'}, gender: 'F', yearOfBirth: 1985, livesIn: 'Oxford', countriesVisited: ['United Kingdom'], languages: [ {name: 'English', proficiency: 'Fluent'} ], married: 'No' } ) db.persons.insert ( { name: {first: 'Sapna', last: 'Chetri'}, gender: 'F', yearOfBirth: 1989, livesIn: 'Siliguri', countriesVisited: ['India'], languages: [ {name: 'Nepali', proficiency: 'Fluent'}, {name: 'Hindi', proficiency: 'Fluent'}, {name: 'English', proficiency: 'Intermediate'} ], married: 'No' } ) db.persons.insert ( { name: {first: 'Jenny', middle: 'H', last: 'Jones'}, gender: 'F', yearOfBirth: 1988, livesIn: 'London', countriesVisited: ['United Kingdom'], languages: [ {name: 'English', proficiency: 'Fluent'} ], married: 'Yes' } ) db.persons.insert ( { name: {first: 'Lakhpa', last: 'Sherpa'}, gender: 'M', yearOfBirth: 1989, lievsIn: 'Gangtok', countriesVisited: ['India'], languages: [ {name: 'Nepali', proficiency: 'Fluent'}, {name: 'English', proficiency: 'Fluent'}, {name: 'Hindi', proficiency: 'Intermediate'} ], married: 'No' } ) db.persons.insert ( { name: {first: 'Anita', last: 'Gogia'}, gender: 'F', yearOfBirth: 1965, livesIn: 'New Delhi', countriesVisited: ['India', 'United Kingdom', 'Canada', 'United States of America'], languages: [ {name: 'Hindi', proficiency: 'Fluent'}, {name: 'Punjabi', proficiency: 'Fluent'}, {name: 'English', proficiency: 'Fluent'} ], married: 'Yes' } ) db.persons.insert ( { name: {first: 'Neeharika', last: 'Chauhan'}, gender: 'F', yearOfBirth: 1988, livesIn: 'Jaipur', countriesVisited: ['India'], languages: [ {name: 'Hindi', proficiency: 'Fluent'}, {name: 'English', proficiency: 'Intermediate'} ], married: 'No' } ) db.persons.insert ( { name: {first: 'Nancy', last: 'Karin'}, gender: 'F', yearOfBirth: 1992, livesIn: 'Montreal', countriesVisited: ['Canada', 'United States of America'], languages: [ {name: 'English', proficiency: 'Fluent'}, {name: 'French', proficiency: 'Fluent'}, {name: 'German', proficiency: 'Intermediate'}, {name: 'Greek', proficiency: 'Intermediate'}, {name: 'Latin', proficiency: 'Intermediate'}, {name: 'Sanskrit', proficiency: 'Beginner'} ] } ) db.persons.count() -- Find all females db.persons.find( {gender: 'F'} ) db.persons.find( {gender: 'F'} ).pretty() db.persons.find( {gender: 'F'} ).count() db.persons.find( {gender: 'F'}, {name: 1} ) db.persons.find( {gender: 'F'}, {name: 1, _id: 0} ) db.persons.find( {gender: 'F'}, {name: true, _id: false} ) db.persons.find( {gender: 'F'}, {name: 1, yearOfBirth: 1} ) db.persons.find( {gender: 'F'}, {name: 1, yearOfBirth: 1, _id: 0} ) db.persons.find( {gender: 'F'}, {name: 1, yearOfBirth: 1, _id: 0, married: 0} ) // This gives an error: "cannot mix including and excluding fields". -- Find names of all persons db.persons.find ( {}, {name: 1, _id: 0} ) -- Find all people living in Mumbai or Jaipur db.persons.find ( {livesIn: {$in: ['Mumbai', 'Jaipur'] } } ).pretty() db.persons.find ( {livesIn: {$in: ['Mumbai', 'Jaipur'] } }, {name: 1, _id: 0} ).pretty() -- All people born before 1980 db.persons.find ( {yearOfBirth: {$lt: 1980} }, {name: 1, yearOfBirth: 1, _id: 0} ).pretty() -- All persons not living in Jaipur db.persons.find ( {livesIn: {$ne: 'Jaipur'} }, {name: 1, livesIn: 1, _id: 0} ).pretty() -- All persons who have visited India, United States of America and Singapore db.persons.find ( {countriesVisited: {$all: ['India', 'United States of America', 'Singapore'] } }, {name: 1, countriesVisited: 1, _id: 0} ).pretty() -- Find all persons with first name as 'Sapna' db.persons.find ( {'name.first' : 'Sapna'}, {name: 1, _id: 0} ).pretty() -- Find all persons whose 'name' subdocument has exactly the same fields, with the given values, in the same order db.persons.find ( {name: {first: 'Jenny', last: 'Jones'} }, {name: 1, _id: 0} ) -- Find all persons with 'Jenny' as first name and 'Jones' as last name db.persons.find ( {'name.first': 'Jenny', 'name.last': 'Jones'}, {name: 1, _id: 0} ) -- Find all persons who have visited India db.persons.find ( {countriesVisited: 'India'}, {name: 1, countriesVisited: 1, _id: 0} ).pretty() -- Find all persons who have visited Canada and United States of America. But the following query returns only those documents -- where the data is exactly as given below and in the same order. db.persons.find ( {countriesVisited: ['Canada', 'United States of America'] }, {name: 1, countriesVisited: 1, _id: 0} ).pretty() -- This is the correct query. Though if you were looking for those people who have visited only these two countries and -- no other country, this will not work. db.persons.find ( {countriesVisited: {$all: ['Canada', 'United States of America'] } }, {name: 1, _id: 0} ) -- Find all persons whose first country visited is India db.persons.find ( {'countriesVisited.0': 'India'}, {name: 1, countriesVisited: 1, _id: 0} ).pretty() -- Find all persons whose first language is 'English' db.persons.find ( {'languages.0.name': 'English'}, {name: 1, languages: 1, _id: 0} ) -- Find all persons who know English db.persons.find ( {'languages.name': 'English'}, {name: 1, languages: 1, _id: 0} ) -- Find all persons who either live in Mumbai or have visited India db.persons.find ( {$or: [ {livesIn: 'Mumbai'}, {countriesVisited: 'India'} ] }, {name: 1, livesIn: 1, countriesVisited: 1, _id: 0} ).pretty() -- Find all persons who have visited India or know Hindi db.persons.find ( {$or: [ {countriesVisited: 'India'}, {'languages.name': 'Hindi'} ] }, {name: 1, countriesVisited: 1, languages: 1, _id: 0} ).pretty() -- Find all persons who have visited India and know Hindi db.persons.find ( {$and: [ {countriesVisited: 'India'}, {'languages.name': 'Hindi'} ] }, {name: 1, countriesVisited: 1, languages: 1, _id: 0} ).pretty() -- Find all persons who are fluent in English. -- The following is incorrect: it will find all persons who know English and are fluent in any one langauge. db.persons.find ( {$and: [ {'languages.name': 'English'}, {'languages.proficiency': 'Fluent'} ] }, {name: 1, _id: 0} ) -- What we need is to compare language name and proficiency level within the same array element. This is the right query: db.persons.find ( {languages: {$elemMatch: {name: 'English', proficiency: 'Fluent'} } }, {name: 1, languages: 1, _id: 0} ).pretty() -- Find all persons with fluency in English or Hindi db.persons.find ( {languages: {$elemMatch: { name: {$in: ['English', 'Hindi'] }, proficiency: 'Fluent' } } }, {name: 1, languages: 1, _id: 0} ).pretty() -- Find all persons who have visited India and are fluent in Hindi db.persons.find ( {countriesVisited: 'India', languages: {$elemMatch: {name: 'Hindi', proficiency: 'Fluent'} } }, {name: 1, countriesVisited: 1, languages: 1, _id: 0} ) -- All males born before 1970 and all females born before 1980. db.persons.find ( {$or: [ {gender: 'M', yearOfBirth: {$lt: 1970} }, {gender: 'F', yearOfBirth: {$lt: 1980} } ] }, {name: 1, gender: 1, yearOfBirth: 1, _id: 0} ) -- For all females, return their name and the first three countries visited by them db.persons.find ( {gender: 'F'}, {name: 1, countriesVisited: {$slice: 3}, _id: 0} ) -- Show all persons, sorted in ascending order of their year of birth db.persons.find( {}, {name: 1, yearOfBirth: 1, _id: 0} ).sort ( {yearOfBirth: 1} ) -- Show all persons, sorted in ascending order of their year of birth; suppress people with no yearOfBirth available. db.persons.find ( {yearOfBirth: {$exists: true} }, {name: 1, yearOfBirth: 1, _id: 0} ).sort ( {yearOfBirth: 1} ) -- Show all persons, sorted in descending order of their year of birth db.persons.find ( {yearOfBirth: {$exists: true} }, {name: 1, yearOfBirth: 1, _id: 0} ).sort ( {yearOfBirth: -1} ) -- Show all persons, sorted by last name and then first name db.persons.find ( {}, {name: 1, _id: 0} ).sort ( {'name.last': 1, 'name.first': 1} ) -- Show persons, sorted by last name and then first name, limiting to first five people db.persons.find ( {}, {name: 1, _id: 0} ).sort ( {'name.last': 1, 'name.first': 1} ).limit (5) -- The following also gives the same output even though limit is written before sort. MongoDB shell always applies -- limit clause after sort clause. db.persons.find ( {}, {name: 1, _id: 0} ).limit(5).sort ( {'name.last': 1, 'name.first': 1} ) -- Show persons, sorted by last name and then first name, limiting to 6-10th persons only db.persons.find ( {}, {name: 1, _id: 0} ).sort ( {'name.last': 1, 'name.first': 1} ).skip(5).limit (5) -- Find and return only one document for a condition db.persons.findOne ( {gender: 'F'}, {name: 1, _id: 0} ) -- The following, however, is an error: sort, limit, skip are not applicable with findOne. db.persons.findOne ( {gender: 'F'}, {name: 1, _id: 0} ).sort ( {'yearOfBirth': 1} ) -- Specifying the _id field db.persons.insert ( {_id: 101, name: {first: 'Robinson', last: 'Crusoe'} } ) -- Duplicate key error: db.persons.insert ( {_id: 101, name: {first: 'Jonathan', middle: 'Livingstone', last: 'Seagull'} } ) -- _id can be a document itself db.persons.insert ( {_id: {name: {first: 'Mary', last: 'Kym'}}, gender: 'F'}) -- Update. The following is bad as after this command the document only has the "married" field. db.persons.update ( {name: {first: 'Nancy', last: 'Karin'} }, {married: 'Y'} ) -- Let's reset it first to the original data: db.persons.remove( {"_id" : ObjectId("523c0c8c378c940550e7cb43") } ) db.persons.insert ( { name: {first: 'Nancy', last: 'Karin'}, gender: 'F', yearOfBirth: 1992, livesIn: 'Montreal', countriesVisited: ['Canada', 'United States of America'], languages: [ {name: 'English', proficiency: 'Fluent'}, {name: 'French', proficiency: 'Fluent'}, {name: 'German', proficiency: 'Intermediate'}, {name: 'Greek', proficiency: 'Intermediate'}, {name: 'Latin', proficiency: 'Intermediate'}, {name: 'Sanskrit', proficiency: 'Beginner'} ] } ) db.persons.update ( {name: {first: 'Nancy', last: 'Karin'} }, {name: {first: 'Nancy', last: 'Karin'}, gender: 'F', yearOfBirth: 1992, livesIn: 'Montreal', countriesVisited: ['Canada', 'United States of America'], languages: [ {name: 'English', proficiency: 'Fluent'}, {name: 'French', proficiency: 'Fluent'}, {name: 'German', proficiency: 'Intermediate'}, {name: 'Greek', proficiency: 'Intermediate'}, {name: 'Latin', proficiency: 'Intermediate'}, {name: 'Sanskrit', proficiency: 'Beginner'} ], married: 'Y' } ) -- But this is cumbersome. Better would be: db.persons.update ( {name: {first: 'Nancy', last: 'Karin'} }, {$set: {married: 'N'} } ) -- There is no such record, so collections remains unaffected, no error is returned either. db.persons.update ( {name: {first: 'Merilyn', last: 'Holmes'} }, {$set: {gender: 'F', yearOfBirth: 1997, married: 'N'} } ) -- This illustrates upsert operation db.persons.update ( {name: {first: 'Merilyn', last: 'Holmes'} }, {$set: {gender: 'F', yearOfBirth: 1997, married: 'N'} }, {upsert: true} ) -- Single record update db.persons.update ( {'countriesVisited.0': 'India'}, {$set: {nationality: 'Indian'} } ) -- Multiple records update db.persons.update ( {'countriesVisited.0': 'India'}, {$set: {nationality: 'Indian'} }, {multi: true} ) -- Remove a field from a document db.persons.update ( {name: {first: 'Nancy', last: 'Karin'} }, {$unset: {married: 1} } ) -- Set the first language of Nancy Karin to Portugese db.persons.update ( {name: {first: 'Nancy', last: 'Karin'} }, {$set: {'languages.0.name': 'Portugese'} } ) -- Just a placeholder for a frequent query. Not required to be part of documentation. db.persons.find ({'name.first': 'Rita', 'name.last': 'Chauhan'} ).pretty() -- Change Nancy Karin's language from German to Italian db.persons.update ( {'name.first': 'Nancy', 'name.last': 'Karin', 'languages.name': 'German'}, {$set: {'languages.$.name': 'Italian'} } ) -- Change Italian back to German, and make her fluent in German db.persons.update ( {name: {first: 'Nancy', last: 'Karin'}, 'languages.name': 'Italian'}, {$set: {'languages.$': {name: 'German', proficiency: 'Expert'} } } ) -- Adding Beginner level proficiency in Spanish for Rita Chauhan db.persons.update ( {'name.first': 'Rita', 'name.last': 'Chauhan'}, {$push: {languages: {name: 'Spanish', proficiency: 'Beginner' } } } ) -- Increment / set a field value db.persons.update ( {'name.first': 'Rita', 'name.last': 'Chauhan'}, {$inc: {children: 1} } ) -- Renaming a field. db.persons.find ( {livesIn: 'Gangtok'} ) -- The above does not find a record. Reason: wrongly spelt field name as 'lievsIn' for one record. Let's fix that db.persons.update ( {}, {$rename: {'lievsIn': 'livesIn'}}, {multi: true} ) -- Similarly, subdocument fields can be renamed also db.persons.update ( {}, {$rename: {'name.first': 'name.fname'} }, {multi: true} ) -- Roll it back db.persons.update ( {}, {$rename: {'name.fname': 'name.first'} }, {multi: true} ) -- Renaming multiple field names db.persons.update ( {}, {$rename: {'name.first': 'name.fname', 'name.last': 'name.lname', yearOfBirth: 'birthYear'} }, {multi: true} ) -- Roll it back db.persons.update ( {}, {$rename: {'name.fname': 'name.first', 'name.lname': 'name.last', birthYear: 'yearOfBirth'} }, {multi: true} ) -- Fields can be renamed / moved outside a subdocument an vice-verse also. db.persons.update ( {}, {$rename: {'name.first': 'firstname', 'name.last': 'lastname'} }, {multi: true} ) -- Drop the empty "name" subdocument. db.persons.update ( {}, {$unset: {name: 'anyjunkcanbegivenhere doesnotmatter'} }, {multi: true} ) -- Roll back everything. db.persons.update ( {}, {$rename: {firstname: 'name.first', lastname: 'name.last'} }, {multi: true} ) -- Narayan Sundaram, Rita Chauhan and Liza Bordelon have enrolled for Sanskrit classes, so they should all be marked as having -- beginner level proficiency in Sanskrit. Out of these Liza Bordelon already has this, so should not be duplicated. db.persons.update ( {name: {$in: [ {first: 'Narayan', last: 'Sundaram'}, {first: 'Rita', last: 'Chauhan'}, {first: 'Liza', last: 'Bordelon'} ] } }, {$addToSet: {languages: {name: 'Sanskrit', proficiency: 'Beginner'} } }, {multi: true} ) -- Add multiple languages to Neeharika Chauhan db.persons.update ( {'name.first': 'Neeharika', 'name.last': 'Chauhan'}, {$push: {languages: {$each: [ {name: 'Marathi', proficiency: 'Fluent'}, {name: 'Gujrati'}, {name: 'Punjabi', proficiency: 'Intermediate'} ] } } } ) -- Remove the last language for Nancy Karin db.persons.update ( {'name.first': 'Nancy', 'name.last': 'Karin'}, {$pop: {languages: 1}}) -- Remove the first language for Nancy Karin db.persons.update ( {'name.first': 'Nancy', 'name.last': 'Karin'}, {$pop: {languages: -1}}) -- Add languages for Nancy Karin and keep the data sorted db.persons.update ( {'name.first': 'Nancy', 'name.last': 'Karin'}, {$push: {languages: {$each: [ {name: 'Marathi', proficiency: 'Beginner'}, {name: 'Gujrati', proficiency: 'Beginner'} ], $sort: {name: 1} } } } ) -- Add some more languages for Nancy Karin, sort the data, and keep only the first three db.persons.update ( {'name.first': 'Nancy', 'name.last': 'Karin'}, {$push: {languages: {$each: [ {name: 'Sindhi', proficiency: 'Beginner'}, {name: 'Tullu', proficiency: 'Beginner'} ], $sort: {name: 1}, $slice: 3 } } } ) -- For all documents in the 'person” collection, we want the array “languages” to be sorted by language name. db.persons.update ( {}, {$push: {languages: {$each: [], $sort: {name: 1} } } }, {multi: true} ) -- save operation db.persons.save ( {name: {first: 'Jonathan', middle: 'Livingstone', last: 'Seagull'} } ) db.persons.save ( {_id: 102, name: {first: 'Vishnudutt', last: 'mark'} } ) db.persons.save ( {_id: 102, gender: 'M'} ) db.persons.save ( {_id: 102, name: {first: 'Vishnudutt', last: 'mark'}, gender: 'M'} ) -- findAndModify db.persons.findAndModify ( { query: {$and: [{'name.first': 'Zoa'}, {'name.last': 'Hendrik'}]}, remove: true, update: {$set: {livesIn: 'Madrid'} } } ) db.persons.findAndModify ( { query: {'name.first': 'Zoa'}, update: {$set {yearOfBirth: 1950} } } ) -- Remove all males db.persons.remove ( {gender: 'M'} ) -- Drop the collection; copy all data back into the collection -- Remove just one male db.persons.remove ( {gender: 'M'}, 1) -- Remove all documents db.persons.remove() -- Better to drop the collection db.persons.drop() +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Schema design +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- Blog project db.users.insert ( {_id: "andrew", name: {first: "Andrew", last: "Weiss"}, email: "andrew@example.com"} ) db.users.insert ( {_id: "bharat", name: {first: "Bharat", last: "Kumar"}, email: "bharat@example.com"} ) db.users.insert ( {_id: "charlie", name: {first: "Charlie", last: "Gordon"}, email: "charlie@example.com"} ) db.users.insert ( {_id: "dilshad", name: {first: "Dilshad", last: "Khan"}, email: "dilshad@example.com"} ) db.users.insert ( {_id: "espicioza", name: {first: "Espicioza"}, email: "espy@example.com"} ) db.users.insert ( {_id: "froze", name: {first: "Feroze", last: "Sheikh"}, email: "froze@example.com"} ) db.users.insert { {_id: "grv", name: {first: "Gurvinder", last: "Singh"}, email: "gurvindersingh@example.com"} ) db.users.insert ( {_id: "hari", name: {first: "harish", last: "pande"}, email: "hpande@example.com"} ) db.users.insert ( {_id: "isabelle", name: {first: "isabelle"}, email: "isabelle@example.com"} ) db.users.insert ( {_id: "jc", {name: {first: "janet", last: "coelho"}, email: "jcoelho@example.com"} ) -- Relational style design db.posts.insert ( { _id: 1, title: "My First Post", author: "charlie", date: new Date ("25-Apr-2014"), post: "This is my first post and I have no idea what to write" } ) db.comments.insert( { _id: 1, postid: 1, author: "andrew", comment: "this is funny", order: 1 } ) --------- Right design db.posts.insert ( { _id: 1, title: "My First Post", author: "charlie", date: new Date ("25-Apr-2014"), post: "This is my first post and I have no idea what to write", comments: [ {user: "andrew", comment: "this is funny"}, {user: "dilshad", comment: "Welcome to the world of blogging, Charlie"} ] } ) db.citizens.insert ( {name: "kamal", city: "Mumbai"} ) db.cities.insert ( {_id: "Mumbai", population: "1.4 million"} ) db.students.insert ( {_id: 1, name: "Amar", courses: [2, 3, 4] } ) db.students.insert ( {_id: 2, name: "Akbar", courses: [1, 2] } ) db.students.insert ( {_id: 3, name: "Anthony"} ) db.students.insert ( {_id: 4, name: "Seeta", courses: [1, 4] } ) db.students.insert ( {_id: 5, name: "Geeta", courses: [3] } ) db.courses.insert ( {_id: 1, title: "African History", students: [2, 4] } ) db.courses.insert ( {_id: 2, title: "Anthropology", students: [1, 2] } ) db.courses.insert ( {_id: 3, title: "Indian Constitution", students: [1, 5] } ) db.courses.insert ( {_id: 4, title: "European Geography", students: [1, 4] } ) db.courses.insert ( {_id: 5, title: "Space Exploration"} ) db.categories.insert ( {_id: 1, name: "Clothing and accessories"} ) db.categories.insert ( {_id: 2, name: "Boys", ancestors: [1] } ) db.categories.insert ( {_id: 3, name: "Shirts", ancestors: [1, 2] } ) db.products.insert ( {_id: 1, name: "612 Ivy League Boys Shirts", price: 799, category: 3} ) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Schema design: Event Logger +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ db.events.insert ( {host: '127.0.0.1', user: 'amar', time: ISODate("2015-05-01T20:06:17Z"), path: '/cis/cis.pdf', request: 'GET /cis/cis.pdf HTTP/1.0', status: 200, response_size: 2103} ) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Capped collection +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ db.createCollection ('eventlog', {capped: true, size: 64*1024, max: 32} ) db.eventlog.insert ( {description: 'some dummy event'}) db.eventlog.insert ( {description: 'another dummy event'}) db.eventlog.find().sort( {natural: -1}) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Indexes +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ db.persons.ensureIndex ( {livesIn: 1} ) db.persons.ensureIndex ( {yearOfBirth: 1} ) db.persons.getIndexes () db.system.indexes.find() db.persons.dropIndex ( {yearOfBirth: 1} ) db.persons.ensureIndex ( {yearOfBirth: -1} ) db.persons.ensureIndex ( {gender: 1, yearOfBirth: -1} ) db.persons.ensureIndex ( {name: 1} ) db.persons.ensureIndex ( {'name.last': 1, 'name.first': 1, yearOfBirth: -1} ) db.persons.ensureIndex ( {married: 1}, {sparse: true} ) db.persons.ensureIndex ( {name: 1}, {unique: true} ) db.persons.ensureIndex ( {'name.last': 1, 'name.first': 1}, {unique: true} ) -- fails as there are two Jenny Jones'es. db.persons.ensureIndex ( {'name.last': 1, 'name.first': 1}, {unique: true, dropDups: true} ) db.persons.ensureIndex ( {'name.last': 1}, {background: true} ) db.persons.find ( {livesIn: 'Mumbai'}, {name: 1} ).explain() db.persons.find ( {}, {name:1} ).sort ( {'name.last': 1} ).explain() db.persons.find ( {}, {name: 1} ).sort ( {'name.first': 1} ).explain() db.persons.find ( {}, {'name.last': 1} ).sort ( {'name.last': 1} ).explain() db.persons.find ( {}, {'name.last': 1, gender: 1, 'name.first': 1} ).hint ( {'name.last': 1, 'name.first': 1} ).explain() db.persons.find ( {}, {'name.last': 1} ).sort ( {'name.last': 1} ).hint ( {$natural: 1} ).explain() +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Replica Set, on one physical machine, different processes +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- create folders c:\data\rs1\rs1.1, c:\data\rs1\rs1.2, c:\data\rs1\rs1.3 -- In three separate windows, run the following commands: mongod --port 27001 --dbpath c:\data\rs1\rs1.1 --replSet rs1 mongod --port 27002 --dbpath c:\data\rs1\rs1.2 --replSet rs1 mongod --port 27003 --dbpath c:\data\rs1\rs1.3 --replSet rs1 -- In another window, open mongo shell and connect to the first mongod instance: mongo --port 27001 -- Initiate the replicata set rsconf = { _id: "rs1", members: [ {_id: 1, host: "localhost:27001"} ] } rs.initiate (rsconf) rs.conf () rs.add ("localhost:27002") rs.add ("localhost:27003") rs.status() -- You may insert some documents using the mongo shell. db.persons.count() -- In another window, open mongo shell and connect to the second mongod instance: mongo --port 27002 -- The following, however, will give error as this is not a primary db.persons.count() -- Try to insert a record, will fail also -- However, you may read from the secondary: rs.slaveOk() -- Now the following works db.persons.count() -- In another window, open mongo shell and connect to the second mongod instance: mongo --port 27003 db.persons.count() rs.slaveOk() db.persons.count() -- Stop the primary mongod instance on port 27001 -- Come to mongo shell connected to 27001 and the following commands give error as the corresponding server is down. rs.status() db.persons.count() -- Come to mongo shell connected to 27002 and 27003 and give the same commands rs.status() db.persons.count() -- In the new primary mongo shell insert one record db.persons.insert ( {name: {first: 'Eva'} } ) db.persons.count() -- Start mongod on 27001 again mongod --port 27001 --dbpath c:\data\rs1\rs1.1 --replSet rs1 -- and all is well again. -- In case of problem: rs.reconfig (rsconf, {force: true} ) -- We want to make 27002 as primary. For this we can make current primary crash or step down, but in an election -- there is no guarantee that 27002 will be elected. So we set priorities: rsconfig = rs.conf () rsconfig.members [0].priority = 2 rsconfig.members [1].priority = 3 rsconfig.members [2].priority = 1 rs.reconfig (rsconfig) -- Without changing priorities, if you want the primary to step down temporarily rs.stepDown () -- Later, to make the highest priority server primary again, go to the new primary and stepDown there: rs.stepDown () -- Converting from a replica set to standalone; on the primary: rs.remove ("localhost:27001") rs.remove ("localhost:27003") -- Then shutdown the mongod instance and start again without --replSet parameter -- How do you reverse the effect of rs.slaveOk ()? -- How do you convert a replica set to a standalone? Even after removing all other members, when I start mongod, I get the error: -- "mongod started without --replSet yet 1 documents are present in local.system.replset -- Restart with --replSet unless you are doing maintenance and no other clients are connected. -- The TTL collection monitor will not start because of this. -- For more info see http://dochub.mongodb.org/core/ttlcollections +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Converting an existing standalone to a replica set +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- create folders c:\data\rs2\rs2.1, c:\data\rs2\rs2.2, c:\data\rs2\rs2.3 -- To be on the safe side, we'll copy an existing standalone database (c:\data\db) to c:\data\rs2\rs2.1 +++++++++++++++++++++++ + Replica Set, on three physical machines +++++++++++++++++++++++ -- machine 1, 172.16.11.19 -- machine 2, 172.16.11.250 -- machine 3, 172.16.11.244 -- client machine, 172.16.11.25 -- machine 1 mongod --replSet rs1 -- machine 2 mongod --replSet rs1 -- machine 3 mongod --replSet rs1 -- client machine mongo 172.16.11.19 rsconfig = {_id: 'rs1', members: [ {_id: 0, host: '172.16.11.19'}, {_id: 1, host: '172.16.11.250'} ] } rs.initiate (rsconfig) rs.conf() rs.status() rs.add ('172.16.11.244') rs.status() ----------------------------------------- - GridFS ----------------------------------------- db.videos.files.find().pretty() db.videos.chunks.find( {}, {data:0}) ------------------------------------------ - Geospatial indexes ------------------------------------------ db.zips.count() db.zips.ensureIndex ( {loc: '2d'} ) db.zips.ensureIndex ( {loc: '2d', state: 1} ) db.zips.find ( {loc: {$near: [-72.622739, 42.070206] } } ) db.zips.find ( {loc: {$near: [-72.622739, 42.070206] } } ).limit(10) db.zips.dropIndex ( {loc: '2d'} ) db.zips.dropIndex ( {loc: '2d', state: 1} ) db.zips.ensureIndex ( {loc: '2dsphere'} ) db.zips.find ( {loc: {$near: { $geometry: { type: 'Point', coordinates: [-87.635915, 41.878876] }, $maxDistance: 100000} } } ) db.stores.find( {loc: {$near: { $geometry: {type: "Point", coordinates: [-130, 39] }, $maxDistance: 1000000 } } } ) ------------------------------- - Text indexes and search ------------------------------- db.idioms.drop() db.idioms.insert ( { idiom: "A Bird In The Hand Is Worth Two In The Bush ", meaning: "Having something that is certain is much better than taking a risk for more, because chances are you might lose everything. " } ) db.idioms.insert ( { idiom: "A Blessing In Disguise ", meaning: "Something good that isn't recognized at first. " } ) db.idioms.insert ( { idiom: "A Chip On Your Shoulder ", meaning: "Being upset for something that happened in the past. " } ) db.idioms.insert ( { idiom: "A Dime A Dozen", meaning: "Anything that is common and easy to get. " } ) db.idioms.insert ( { idiom: "A Doubting Thomas ", meaning: "A skeptic who needs physical or personal evidence in order to believe something. " } ) db.idioms.insert ( { idiom: "A Drop in the Bucket ", meaning: "A very small part of something big or whole. " } ) db.idioms.insert ( { idiom: "A Fool And His Money Are Easily Parted ", meaning: "It's easy for a foolish person to lose his/her money. " } ) db.idioms.insert ( { idiom: "A House Divided Against Itself Cannot Stand ", meaning: "Everyone involved must unify and function together or it will not work out. " } ) db.idioms.insert ( { idiom: "A Leopard Can't Change His Spots ", meaning: "You cannot change who you are. " } ) db.idioms.insert ( { idiom: "A Penny Saved Is A Penny Earned ", meaning: "By not spending money, you are saving money (little by little). " } ) db.idioms.insert ( { idiom: "A Picture Paints a Thousand Words ", meaning: "A visual presentation is far more descriptive than words. " } ) db.idioms.insert ( { idiom: "A Piece of Cake ", meaning: "A task that can be accomplished very easily. " } ) db.idioms.insert ( { idiom: "A Slap on the Wrist ", meaning: "A very mild punishment. " } ) db.idioms.insert ( { idiom: "A Taste Of Your Own Medicine ", meaning: "When you are mistreated the same way you mistreat others. " } ) db.idioms.insert ( { idiom: "A Toss-Up ", meaning: "A result that is still unclear and can go either way. " } ) db.idioms.insert ( { idiom: "Actions Speak Louder Than Words ", meaning: "It's better to actually do something than just talk about it. " } ) db.idioms.insert ( { idiom: "Add Fuel To The Fire ", meaning: "Whenever something is done to make a bad situation even worse than it is. " } ) db.idioms.insert ( { idiom: "Against The Clock ", meaning: "Rushed and short on time. " } ) db.idioms.insert ( { idiom: "All Bark And No Bite ", meaning: "When someone is threatening and/or aggressive but not willing to engage in a fight. " } ) db.idioms.insert ( { idiom: "All Greek to me ", meaning: "Meaningless and incomprehensible like someone who cannot read, speak, or understand any of the Greek language would be. " } ) db.idioms.insert ( { idiom: "All In The Same Boat ", meaning: "When everyone is facing the same challenges. " } ) db.idioms.insert ( { idiom: "An Arm And A Leg ", meaning: "Very expensive. A large amount of money. " } ) db.idioms.insert ( { idiom: "An Axe To Grind ", meaning: "To have a dispute with someone. " } ) db.idioms.insert ( { idiom: "Apple of My Eye ", meaning: "Someone who is cherished above all others. " } ) db.idioms.insert ( { idiom: "As High As A Kite ", meaning: "Anything that is high up in the sky. " } ) db.idioms.insert ( { idiom: "At The Drop Of A Hat ", meaning: "Willing to do something immediately. " } ) db.idioms.insert ( { idiom: "Back Seat Driver ", meaning: "People who criticize from the sidelines, much like someone giving unwanted advice from the back seat of a vehicle to the driver. " } ) db.idioms.insert ( { idiom: "Back To Square One ", meaning: "Having to start all over again. " } ) db.idioms.insert ( { idiom: "Back To The Drawing Board ", meaning: "When an attempt fails and it's time to start all over. " } ) db.idioms.insert ( { idiom: "Baker's Dozen ", meaning: "Thirteen. " } ) db.idioms.insert ( { idiom: "Barking Up The Wrong Tree ", meaning: "A mistake made in something you are trying to achieve. " } ) db.idioms.insert ( { idiom: "Beat A Dead Horse ", meaning: "To force an issue that has already ended. " } ) db.idioms.insert ( { idiom: "Beating Around The Bush ", meaning: "Avoiding the main topic. Not speaking directly about the issue. " } ) db.idioms.insert ( { idiom: "Bend Over Backwards ", meaning: "Do whatever it takes to help. Willing to do anything. " } ) db.idioms.insert ( { idiom: "Between A Rock And A Hard Place ", meaning: "Stuck between two very bad options. " } ) db.idioms.insert ( { idiom: "Bite Off More Than You Can Chew ", meaning: "To take on a task that is way to big. " } ) db.idioms.insert ( { idiom: "Bite Your Tongue ", meaning: "To avoid talking. " } ) db.idioms.insert ( { idiom: "Blood Is Thicker Than Water ", meaning: "The family bond is closer than anything else. " } ) db.idioms.insert ( { idiom: "Blue Moon ", meaning: "A rare event or occurance. " } ) db.idioms.insert ( { idiom: "Break A Leg ", meaning: "A superstitious way to say 'good luck' without saying 'good luck', but rather the opposite. " } ) db.idioms.insert ( { idiom: "Buy A Lemon ", meaning: "To purchase a vehicle that constantly gives problems or stops running after you drive it away. " } ) db.idioms.insert ( { idiom: "Can't Cut The Mustard ", meaning: "Someone who isn't adequate enough to compete or participate. " } ) db.idioms.insert ( { idiom: "Cast Iron Stomach ", meaning: "Someone who has no problems, complications or ill effects with eating anything or drinking anything. " } ) db.idioms.insert ( { idiom: "Charley Horse ", meaning: "Stiffness in the leg / A leg cramp. " } ) db.idioms.insert ( { idiom: "Chew someone out ", meaning: "Verbally scold someone. " } ) db.idioms.insert ( { idiom: "Chip on his Shoulder ", meaning: "Angry today about something that occured in the past. " } ) db.idioms.insert ( { idiom: "Chow Down ", meaning: "To eat. " } ) db.idioms.insert ( { idiom: "Close but no Cigar ", meaning: "To be very near and almost accomplish a goal, but fall short. " } ) db.idioms.insert ( { idiom: "Cock and Bull Story ", meaning: "An unbelievable tale. " } ) db.idioms.insert ( { idiom: "Come Hell Or High Water ", meaning: "Any difficult situation or obstacle. " } ) db.idioms.insert ( { idiom: "Crack Someone Up ", meaning: "To make someone laugh. " } ) db.idioms.insert ( { idiom: "Cross Your Fingers ", meaning: "To hope that something happens the way you want it to. " } ) db.idioms.insert ( { idiom: "Cry Over Spilt Milk ", meaning: "When you complain about a loss from the past. " } ) db.idioms.insert ( { idiom: "Cry Wolf ", meaning: "Intentionally raise a false alarm. " } ) db.idioms.insert ( { idiom: "Cup Of Joe ", meaning: "A cup of coffee. " } ) db.idioms.insert ( { idiom: "Curiosity Killed The Cat ", meaning: "Being Inquisitive can lead you into a dangerous situation. " } ) db.idioms.insert ( { idiom: "Cut to the Chase ", meaning: "Leave out all the unnecessary details and just get to the point. " } ) db.idioms.insert ( { idiom: "Dark Horse ", meaning: "One who was previously unknown and is now prominent. " } ) db.idioms.insert ( { idiom: "Dead Ringer ", meaning: "100% identical. A duplicate. " } ) db.idioms.insert ( { idiom: "Devil's Advocate ", meaning: "Someone who takes a position for the sake of argument without believing in that particular side of the arguement. It can also mean one who presents a counter argument for a position they do believe in, to another debater. " } ) db.idioms.insert ( { idiom: "Dog Days of Summer ", meaning: "The hottest days of the summer season. " } ) db.idioms.insert ( { idiom: "Don't count your chickens before they hatch ", meaning: "Don't rely on it until your sure of it. " } ) db.idioms.insert ( { idiom: "Don't Look A Gift Horse In The Mouth ", meaning: "When someone gives you a gift, don't be ungrateful. " } ) db.idioms.insert ( { idiom: "Don't Put All Your Eggs In One Basket ", meaning: "Do not put all your resources in one possibility. " } ) db.idioms.insert ( { idiom: "Doozy ", meaning: "Something outstanding. " } ) db.idioms.insert ( { idiom: "Down To The Wire ", meaning: "Something that ends at the last minute or last few seconds. " } ) db.idioms.insert ( { idiom: "Drastic Times Call For Drastic Measures ", meaning: "When you are extremely desperate you need to take extremely desperate actions. " } ) db.idioms.insert ( { idiom: "Drink like a fish ", meaning: "To drink very heavily. " } ) db.idioms.insert ( { idiom: "Drive someone up the wall ", meaning: "To irritate and/or annoy very much. " } ) db.idioms.insert ( { idiom: "Dropping Like Flies ", meaning: "A large number of people either falling ill or dying. " } ) db.idioms.insert ( { idiom: "Dry Run ", meaning: "Rehearsal. " } ) db.idioms.insert ( { idiom: "Eighty Six ", meaning: "A certain item is no longer available. Or this idiom can also mean, to throw away. " } ) db.idioms.insert ( { idiom: "Elvis has left the building ", meaning: "The show has come to an end. It's all over. " } ) db.idioms.insert ( { idiom: "Ethnic Cleansing ", meaning: "Killing of a certain ethnic or religious group on a massive scale. " } ) db.idioms.insert ( { idiom: "Every Cloud Has A Silver Lining ", meaning: "Be optomistic, even difficult times will lead to better days. " } ) db.idioms.insert ( { idiom: "Everything But The Kitchen Sink ", meaning: "Almost everything and anything has been included. " } ) db.idioms.insert ( { idiom: "Excuse my French ", meaning: "Please forgive me for cussing. " } ) db.idioms.insert ( { idiom: "Cock and Bull Story ", meaning: "An unbelievable tale. " } ) db.idioms.insert ( { idiom: "Cock and Bull Story ", meaning: "An unbelievable tale. " } ) db.idioms.insert ( { idiom: "Feeding Frenzy ", meaning: "An aggressive attack on someone by a group. " } ) db.idioms.insert ( { idiom: "Field Day ", meaning: "An enjoyable day or circumstance. " } ) db.idioms.insert ( { idiom: "Finding Your Feet ", meaning: "To become more comfortable in whatever you are doing. " } ) db.idioms.insert ( { idiom: "Finger lickin' good ", meaning: "A very tasty food or meal. " } ) db.idioms.insert ( { idiom: "Fixed In Your Ways ", meaning: "Not willing or wanting to change from your normal way of doing something. " } ) db.idioms.insert ( { idiom: "Flash In The Pan ", meaning: "Something that shows potential or looks promising in the beginning but fails to deliver anything in the end. " } ) db.idioms.insert ( { idiom: "Flea Market ", meaning: "A swap meet. A place where people gather to buy and sell inexpensive goods. " } ) db.idioms.insert ( { idiom: "Flesh and Blood ", meaning: "This idiom can mean living material of which people are made of, or it can refer to someone's family. " } ) db.idioms.insert ( { idiom: "Flip The Bird ", meaning: "To raise your middle finger at someone. " } ) db.idioms.insert ( { idiom: "Foam at the Mouth ", meaning: "To be enraged and show it. " } ) db.idioms.insert ( { idiom: "Fools' Gold ", meaning: "Iron pyrites, a worthless rock that resembles real gold. " } ) db.idioms.insert ( { idiom: "French Kiss ", meaning: "An open mouth kiss where tongues touch. " } ) db.idioms.insert ( { idiom: "From Rags To Riches ", meaning: "To go from being very poor to being very wealthy. " } ) db.idioms.insert ( { idiom: "Fuddy-duddy ", meaning: "An old-fashioned and foolish type of person. " } ) db.idioms.insert ( { idiom: "Full Monty ", meaning: "This idiom can mean either, "the whole thing" or "completely nude"." } ) db.idioms.insert ( { idiom: "Funny Farm ", meaning: "A mental institutional facility. " } ) db.idioms.insert ( { idiom: "Get Down to Brass Tacks ", meaning: "To become serious about something. " } ) db.idioms.insert ( { idiom: "Get Over It ", meaning: "To move beyond something that is bothering you. " } ) db.idioms.insert ( { idiom: "Get Up On The Wrong Side Of The Bed ", meaning: "Someone who is having a horrible day. " } ) db.idioms.insert ( { idiom: "Get Your Walking Papers ", meaning: "Get fired from a job. " } ) db.idioms.insert ( { idiom: "Give Him The Slip ", meaning: "To get away from. To escape. " } ) db.idioms.insert ( { idiom: "Go Down Like A Lead Balloon ", meaning: "To be received badly by an audience. " } ) db.idioms.insert ( { idiom: "Go For Broke ", meaning: "To gamble everything you have. " } ) db.idioms.insert ( { idiom: "Go Out On A Limb ", meaning: "Put yourself in a tough position in order to support someone/something. " } ) db.idioms.insert ( { idiom: "Go The Extra Mile ", meaning: "Going above and beyond whatever is required for the task at hand. " } ) db.idioms.insert ( { idiom: "Good Samaritan ", meaning: "Someone who helps others when they are in need, with no discussion for compensation, and no thought of a reward. " } ) db.idioms.insert ( { idiom: "Graveyard Shift ", meaning: "Working hours from about 1200 am to 800 am. The time of the day when most other people are sleeping. " } ) db.idioms.insert ( { idiom: "Great Minds Think Alike ", meaning: "Intelligent people think like each other. " } ) db.idioms.insert ( { idiom: "Green Room ", meaning: "The waiting room, especially for those who are about to go on a tv or radio show. " } ) db.idioms.insert ( { idiom: "Gut Feeling ", meaning: "A personal intuition you get, especially when feel something may not be right. " } ) db.idioms.insert ( { idiom: "Haste Makes Waste ", meaning: "Quickly doing things results in a poor ending. " } ) db.idioms.insert ( { idiom: "Hat Trick ", meaning: "When one player scores three goals in the same hockey game. This idiom can also mean three scores in any other sport, such as 3 homeruns, 3 touchdowns, 3 soccer goals, etc. " } ) db.idioms.insert ( { idiom: "Have an Axe to Grind ", meaning: "To have a dispute with someone. " } ) db.idioms.insert ( { idiom: "He Lost His Head ", meaning: "Angry and overcome by emotions. " } ) db.idioms.insert ( { idiom: "Head Over Heels ", meaning: "Very excited and/or joyful, especially when in love. " } ) db.idioms.insert ( { idiom: "Hell in a Handbasket ", meaning: "Deteriorating and headed for complete disaster. " } ) db.idioms.insert ( { idiom: "High Five ", meaning: "Slapping palms above each others heads as celebration gesture. " } ) db.idioms.insert ( { idiom: "High on the Hog ", meaning: "Living in Luxury. " } ) db.idioms.insert ( { idiom: "Hit The Books ", meaning: "To study, especially for a test or exam. " } ) db.idioms.insert ( { idiom: "Hit The Hay ", meaning: "Go to bed or go to sleep. " } ) db.idioms.insert ( { idiom: "Hit The Nail on the Head ", meaning: "Do something exactly right or say something exactly right. " } ) db.idioms.insert ( { idiom: "Hit The Sack ", meaning: "Go to bed or go to sleep. " } ) db.idioms.insert ( { idiom: "Hocus Pocus ", meaning: "In general, a term used in magic or trickery. " } ) db.idioms.insert ( { idiom: "Hold Your Horses ", meaning: "Be patient. " } ) db.idioms.insert ( { idiom: "Icing On The Cake ", meaning: "When you already have it good and get something on top of what you already have. " } ) db.idioms.insert ( { idiom: "Idle Hands Are The Devil's Tools ", meaning: "You are more likely to get in trouble if you have nothing to do. " } ) db.idioms.insert ( { idiom: "If It's Not One Thing, It's Another ", meaning: "When one thing goes wrong, then another, and another... " } ) db.idioms.insert ( { idiom: "In Like Flynn ", meaning: "To be easily successful, especially when sexual or romantic. " } ) db.idioms.insert ( { idiom: "In The Bag ", meaning: "To have something secured. " } ) db.idioms.insert ( { idiom: "In The Buff ", meaning: "Nude. " } ) db.idioms.insert ( { idiom: "In The Heat Of The Moment ", meaning: "Overwhelmed by what is happening in the moment. " } ) db.idioms.insert ( { idiom: "In Your Face ", meaning: "An aggressive and bold confrontation. " } ) db.idioms.insert ( { idiom: "It Takes Two To Tango ", meaning: "A two person conflict where both people are at fault. " } ) db.idioms.insert ( { idiom: "It's A Small World ", meaning: "You frequently see the same people in different places. " } ) db.idioms.insert ( { idiom: "Its Anyone's Call ", meaning: "A competition where the outcome is difficult to judge or predict. " } ) db.idioms.insert ( { idiom: "Ivy League ", meaning: "Since 1954 the Ivy League has been the following universities Columbia, Brown, Cornell, Dartmouth, Yale, Pennsylvania, Princeton, and Harvard. " } ) db.idioms.insert ( { idiom: "Jaywalk ", meaning: "Crossing the street (from the middle) without using the crosswalk. " } ) db.idioms.insert ( { idiom: "Joshing Me ", meaning: "Tricking me. " } ) db.idioms.insert ( { idiom: "Keep An Eye On Him ", meaning: "You should carefully watch him. " } ) db.idioms.insert ( { idiom: "Keep body and soul together ", meaning: "To earn a sufficient amount of money in order to keep yourself alive . " } ) db.idioms.insert ( { idiom: "Keep your chin up ", meaning: "To remain joyful in a tough situation. " } ) db.idioms.insert ( { idiom: "Kick The Bucket ", meaning: "Die. " } ) db.idioms.insert ( { idiom: "Kitty-corner ", meaning: "Diagonally across. Sometimes called Catty-Corner as well. " } ) db.idioms.insert ( { idiom: "Knee Jerk Reaction ", meaning: "A quick and automatic response. " } ) db.idioms.insert ( { idiom: "Knock On Wood ", meaning: "Knuckle tapping on wood in order to avoid some bad luck. " } ) db.idioms.insert ( { idiom: "Know the Ropes ", meaning: "To understand the details. " } ) db.idioms.insert ( { idiom: "Last but not least ", meaning: "An introduction phrase to let the audience know that the last person mentioned is no less important than those introduced before him/her. " } ) db.idioms.insert ( { idiom: "Lend Me Your Ear ", meaning: "To politely ask for someone's full attention. " } ) db.idioms.insert ( { idiom: "Let Bygones Be Bygones ", meaning: "To forget about a disagreement or arguement. " } ) db.idioms.insert ( { idiom: "Let Sleeping Dogs Lie ", meaning: "To avoid restarting a conflict. " } ) db.idioms.insert ( { idiom: "Let The Cat Out Of The Bag ", meaning: "To share a secret that wasn't suppose to be shared. " } ) db.idioms.insert ( { idiom: "Level playing field ", meaning: "A fair competition where no side has an advantage. " } ) db.idioms.insert ( { idiom: "Like a chicken with its head cut off ", meaning: "To act in a frenzied manner. " } ) db.idioms.insert ( { idiom: "liquor someone up ", meaning: "To get someone drunk. " } ) db.idioms.insert ( { idiom: "Long in the Tooth ", meaning: "Old people (or horses). " } ) db.idioms.insert ( { idiom: "Loose Cannon ", meaning: "Someone who is unpredictable and can cause damage if not kept in check. " } ) db.idioms.insert ( { idiom: "Make No Bones About ", meaning: "To state a fact so there are no doubts or objections. " } ) db.idioms.insert ( { idiom: "Method To My Madness ", meaning: "Strange or crazy actions that appear meaningless but in the end are done for a good reason. " } ) db.idioms.insert ( { idiom: "Mumbo Jumbo ", meaning: "Nonsense or meaningless speech. " } ) db.idioms.insert ( { idiom: "Mum's the word ", meaning: "To keep quiet. To say nothing. " } ) db.idioms.insert ( { idiom: "Nest Egg ", meaning: "Savings set aside for future use. " } ) db.idioms.insert ( { idiom: "Never Bite The Hand That Feeds You ", meaning: "Don't hurt anyone that helps you. " } ) db.idioms.insert ( { idiom: "New kid on the block ", meaning: "Someone new to the group or area. " } ) db.idioms.insert ( { idiom: "New York Minute ", meaning: "A minute that seems to go by quickly, especially in a fast paced environment. " } ) db.idioms.insert ( { idiom: "No Dice ", meaning: "To not agree. To not accept a proposition. " } ) db.idioms.insert ( { idiom: "No Room to Swing a Cat ", meaning: "An unsually small or confined space. " } ) db.idioms.insert ( { idiom: "Not Playing With a Full Deck ", meaning: "Someone who lacks intelligence. " } ) db.idioms.insert ( { idiom: "Off On The Wrong Foot ", meaning: "Getting a bad start on a relationship or task. " } ) db.idioms.insert ( { idiom: "Off The Hook ", meaning: "No longer have to deal with a tough situation. " } ) db.idioms.insert ( { idiom: "Off the Record ", meaning: "Something said in confidence that the one speaking doesn't want attributed to him/her. " } ) db.idioms.insert ( { idiom: "On Pins And Needles ", meaning: "Anxious or nervous, especially in anticipation of something. " } ) db.idioms.insert ( { idiom: "On The Fence ", meaning: "Undecided. " } ) db.idioms.insert ( { idiom: "On The Same Page ", meaning: "When multiple people all agree on the same thing. " } ) db.idioms.insert ( { idiom: "Out Of The Blue ", meaning: "Something that suddenly and unexpectedly occurs. " } ) db.idioms.insert ( { idiom: "Out On A Limb ", meaning: "When someone puts themself in a risky situation. " } ) db.idioms.insert ( { idiom: "Out On The Town ", meaning: "To enjoy yourself by going out. " } ) db.idioms.insert ( { idiom: "Over My Dead Body ", meaning: "When you absolutely will not allow something to happen. " } ) db.idioms.insert ( { idiom: "Over the Top ", meaning: "Very excessive. " } ) db.idioms.insert ( { idiom: "Pass The Buck ", meaning: "Avoid responsibility by giving it to someone else. " } ) db.idioms.insert ( { idiom: "Pedal to the metal ", meaning: "To go full speed, especially while driving a vehicle. " } ) db.idioms.insert ( { idiom: "Peeping Tom ", meaning: "Someone who observes people in the nude or sexually active people, mainly for his own gratification. " } ) db.idioms.insert ( { idiom: "Pick up your ears ", meaning: "To listen very carefully. " } ) db.idioms.insert ( { idiom: "Pig In A Poke ", meaning: "A deal that is made without first examining it. " } ) db.idioms.insert ( { idiom: "Pig Out ", meaning: "To eat alot and eat it quickly. " } ) db.idioms.insert ( { idiom: "Pipe Down ", meaning: "To shut-up or be quiet. " } ) db.idioms.insert ( { idiom: "Practice Makes Perfect ", meaning: "By constantly practicing, you will become better. " } ) db.idioms.insert ( { idiom: "Pull the plug ", meaning: "To stop something. To bring something to an end. " } ) db.idioms.insert ( { idiom: "Pulling Your Leg ", meaning: "Tricking someone as a joke. " } ) db.idioms.insert ( { idiom: "Put a sock in it ", meaning: "To tell noisy person or a group to be quiet. " } ) db.idioms.insert ( { idiom: "Queer the pitch ", meaning: "Destroy or ruin a plan. " } ) db.idioms.insert ( { idiom: "Raincheck ", meaning: "An offer or deal that is declined right now but willing to accept later. " } ) db.idioms.insert ( { idiom: "Raining Cats and Dogs ", meaning: "A very loud and noisy rain storm. " } ) db.idioms.insert ( { idiom: "Ring Fencing ", meaning: "Seperated usual judgement to guarantee protection, especially project funds. " } ) db.idioms.insert ( { idiom: "Rise and Shine ", meaning: "Time to get out of bed and get ready for work/school. " } ) db.idioms.insert ( { idiom: "Rome Was Not Built In One Day ", meaning: "If you want something to be completely properly, then its going to take time. " } ) db.idioms.insert ( { idiom: "Rule Of Thumb ", meaning: "A rough estimate. " } ) db.idioms.insert ( { idiom: "Run out of steam ", meaning: "To be completely out of energy. " } ) db.idioms.insert ( { idiom: "Saved By The Bell ", meaning: "Saved at the last possible moment. " } ) db.idioms.insert ( { idiom: "Scapegoat ", meaning: "Someone else who takes the blame. " } ) db.idioms.insert ( { idiom: "Scot-free ", meaning: "To escape and not have to pay. " } ) db.idioms.insert ( { idiom: "Sick As A Dog ", meaning: "To be very sick (with the flu or a cold). " } ) db.idioms.insert ( { idiom: "Sitting Shotgun ", meaning: "Riding in the front passenger seat of a car. " } ) db.idioms.insert ( { idiom: "Sixth Sense ", meaning: "A paranormal sense that allows you to communicate with the dead. " } ) db.idioms.insert ( { idiom: "Skid Row ", meaning: "The rundown area of a city where the homeless and drug users live. " } ) db.idioms.insert ( { idiom: "Smell A Rat ", meaning: "To detect somone in the group is betraying the others. " } ) db.idioms.insert ( { idiom: "Smell Something Fishy ", meaning: "Detecting that something isn't right and there might be a reason for it. " } ) db.idioms.insert ( { idiom: "Son of a Gun ", meaning: "A scamp. " } ) db.idioms.insert ( { idiom: "Southpaw ", meaning: "Someone who is left-handed. " } ) db.idioms.insert ( { idiom: "Spitting Image ", meaning: "The exact likeness or kind. " } ) db.idioms.insert ( { idiom: "Start From Scratch ", meaning: "To do it all over again from the beginning. " } ) db.idioms.insert ( { idiom: "The Ball Is In Your Court ", meaning: "It is your decision this time. " } ) db.idioms.insert ( { idiom: "The Best Of Both Worlds ", meaning: "There are two choices and you have them both. " } ) db.idioms.insert ( { idiom: "The Bigger They Are The Harder They Fall ", meaning: "While the bigger and stronger opponent might be alot more difficult to beat, when you do they suffer a much bigger loss. " } ) db.idioms.insert ( { idiom: "The Last Straw ", meaning: "When one small burden after another creates an unbearable situation, the last straw is the last small burden that one can take. " } ) db.idioms.insert ( { idiom: "The Whole Nine Yards ", meaning: "Everything. All of it. " } ) db.idioms.insert ( { idiom: "Third times a charm ", meaning: "After no success the first two times, the third try is a lucky one. " } ) db.idioms.insert ( { idiom: "Tie the knot ", meaning: "To get married. " } ) db.idioms.insert ( { idiom: "Til the cows come home ", meaning: "A long time. " } ) db.idioms.insert ( { idiom: "To Make A Long Story Short ", meaning: "Something someone would say during a long and boring story in order to keep his/her audience from losing attention. Usually the story isn't shortened. " } ) db.idioms.insert ( { idiom: "To Steal Someone's Thunder ", meaning: "To take the credit for something someone else did. " } ) db.idioms.insert ( { idiom: "Tongue-in-cheek ", meaning: "humor, not to be taken serious. " } ) db.idioms.insert ( { idiom: "Turn A Blind Eye ", meaning: "Refuse to acknowledge something you know is real or legit. " } ) db.idioms.insert ( { idiom: "Twenty three skidoo ", meaning: "To be turned away. " } ) db.idioms.insert ( { idiom: "Under the weather ", meaning: "Feeling ill or sick. " } ) db.idioms.insert ( { idiom: "Up a blind alley ", meaning: "Going down a course of action that leads to a bad outcome. " } ) db.idioms.insert ( { idiom: "Use Your Loaf ", meaning: "Use your head. Think smart. " } ) db.idioms.insert ( { idiom: "Van Gogh's ear for music ", meaning: "Tone deaf. " } ) db.idioms.insert ( { idiom: "Variety Is The Spice Of Life ", meaning: "The more experiences you try the more exciting life can be. " } ) db.idioms.insert ( { idiom: "Wag the Dog ", meaning: "A diversion away from something of greater importance. " } ) db.idioms.insert ( { idiom: "Water Under The Bridge ", meaning: "Anything from the past that isn't significant or important anymore. " } ) db.idioms.insert ( { idiom: "Wear Your Heart On Your Sleeve ", meaning: "To openly and freely express your emotions. " } ) db.idioms.insert ( { idiom: "When It Rains, It Pours ", meaning: "Since it rarely rains, when it does it will be a huge storm. " } ) db.idioms.insert ( { idiom: "When Pigs Fly ", meaning: "Something that will never ever happen. " } ) db.idioms.insert ( { idiom: "Wild and Woolly ", meaning: "Uncultured and without laws. " } ) db.idioms.insert ( { idiom: "Wine and Dine ", meaning: "When somebody is treated to an expensive meal. " } ) db.idioms.insert ( { idiom: "Without A Doubt ", meaning: "For certain. " } ) db.idioms.insert ( { idiom: "X marks the spot ", meaning: "A phrase that is said when someone finds something he/she has been looking for. " } ) db.idioms.insert ( { idiom: "You Are What You Eat ", meaning: "In order to stay healthy you must eat healthy foods. " } ) db.idioms.insert ( { idiom: "You Can't Judge A Book By Its Cover ", meaning: "Decisions shouldn't be made primarily on appearance. " } ) db.idioms.insert ( { idiom: "You Can't Take it With You ", meaning: "Enjoy what you have and not what you don't have, since when you die you cannot take things (such as money) with you. " } ) db.idioms.insert ( { idiom: "Your Guess Is As Good As Mine ", meaning: "I have no idea. " } ) db.idioms.insert ( { idiom: "Zero Tolerance", meaning: "No crime or law breaking big or small will be overlooked. " } ) db.idioms.count() db.idioms.ensureIndex ( {idiom: 'text'} ) db.idioms.find ( {$text: {$search: 'book'} } ) db.idioms.find ( {$text: {$search: 'book cover'} } ) db.idioms.find ( {$text: {$search: 'cat'} } ) db.idioms.find ( {$text: {$search: 'cat water'} } ) -- Following will give error; you can have only one index of type 'text' for a collection. db.idioms.ensureIndex ( {meaning: 'text'} ) -- But you can do the following: db.idioms.getIndexes() db.idioms.dropIndex ("idiom_text") db.idioms.ensureIndex ( {idiom: 'text', meaning: 'text'} ) db.idioms.find ( {$text: {$search: 'book crime'} } ) -- Also db.idioms.getIndexes() db.idioms.dropIndex ("idiom_text_meaning_text") db.idioms.ensureIndex ( {'$**': 'text'}, {name: 'FullTextIndex'} ) db.idioms.find ( {$text: {$search: 'book crime'} }, {score: {$meta: 'textScore'} } ).sort ( {score: {$meta: 'textScore'} } ) -- Assigning weights db.idioms.dropIndex ('FullTextIndex') db.idioms.ensureIndex ( {idiom: 'text', meaning: 'text'}, {weights: {idiom: 5, meaning: 1}}, {name: 'TextIndex'} ) year student_id physics chemistry maths year 2010: 0974195 students year 2011: 0984222 students year 2012: 1105429 students year 2013: 1407822 students for each of the above years for students_id from 1 to number of students for each of the three subjects generate two random numbers between 0 and 100 and take their average as the marks of the student {year: 2010, student_id: 1, physics: 60, chemistry: 65, maths: 70} ---------------------------------- - Aggregation framework ---------------------------------- -- All ZIPs with population >= 10000 db.zips.find( {pop: {$gte: 10000}}).pretty() -- State-wise count of ZIP codes db.zips.aggregate ( {$group: {_id: '$state', zip_count: {$sum: 1}}} ).pretty() -- Or db.zips.aggregate ( {$group: {_id: {state: '$state'}, zip_count: {$sum: 1}}} ).pretty() -- Sort the above in descending order of zip_count db.zips.aggregate ( {$group: {_id: '$state', zip_count: {$sum: 1}}}, {$sort: {zip_count: -1}} ).pretty() -- Sort the above in ascending order of state code db.zips.aggregate ( {$group: {_id: '$state', zip_count: {$sum: 1}}}, {$sort: {_id: 1}} ).pretty() -- Compound grouping db.zips.aggregate ( {$group: {_id: {city: '$city', state: '$state'}, zip_count: {$sum: 1}}} ).pretty() -- State-wise sum of population db.zips.aggregate ( {$group: {_id: '$state', population: {$sum: '$pop'}}} ).pretty() -- Sort the above in descending order of population db.zips.aggregate ( {$group: {_id: '$state', population: {$sum: '$pop'}}}, {$sort: {population: -1}} ).pretty() -- Sort the above in ascending order of state code db.zips.aggregate ( {$group: {_id: '$state', population: {$sum: '$pop'}}}, {$sort: {_id: 1}} ).pretty() -- Restrict the above list to states with population of 10 million or more db.zips.aggregate ( {$group: {_id: '$state', population: {$sum: '$pop'}}}, {$match: {population: {$gte: 10*1000*1000}}}, {$sort: {_id: 1}} ).pretty() -- One document per state, with list of its cities in an array inside it db.zips.aggregate ( {$group: {_id: '$state', cities: {$addToSet: '$city'}}} ).pretty() -- For each state print the population of the zip code with the highest population in each state. db.zips.aggregate ( {$group: {_id: '$state', pop: {$max: '$pop'} } } ) -- Also include the minimum population in the above result db.zips.aggregate ( {$group: {_id: '$state', max: {$max: '$pop'}, min: {$min: '$pop'} } } ) -- Transform the data with city + state, zip, population fields db.zips.aggregate ( {$project: {_id: 0, city_state: {$concat: ['$city', ', ', '$state'] }, zip: '$_id', population: '$pop' } } ) -- Same as above, but with city + state converted to lower case db.zips.aggregate ( {$project: {_id: 0, city_state: {$toLower: {$concat: ['$city', ', ', '$state'] } }, zip: '$_id', population: '$pop' } } ) -- For the state of Florida, get city-wise population and list of zip codes db.zips.aggregate ( {$match: {state: 'FL'} }, {$group: {_id: '$city', population: {$sum: '$pop'}, zips: {$addToSet: '$_id'} } } ) -- Sort the above in descending order of population db.zips.aggregate ( {$match: {state: 'FL'} }, {$group: {_id: '$city', population: {$sum: '$pop'}, zips: {$addToSet: '$_id'} } }, {$sort: {population: -1} } ) -- Just sorting by state name, city name (both ascending) db.zips.aggregate ( {$sort: {state: 1, city: 1} } ) -- Skip and limit db.zips.aggregate ( {$sort: {state: 1, city: 1} }, {$skip: 3}, {$limit: 2} ) -- State-wise list of the city with largest population db.zips.aggregate ( {$group: {_id: {state: '$state', city: '$city'}, population: {$sum: '$pop'} } }, {$sort: {'_id.state': 1, population: -1} }, {$group: {_id: {state: '$_id.state'}, city: {$first: '$_id.city'}, population: {$first: '$population'}} }, {$sort: {population: -1} } ) -- State-wise list of the biggest and smallest cities (by population) db.zips.aggregate ( {$group: {_id: {state: '$state', city: '$city'}, population: {$sum: '$pop'} } }, {$sort: {'_id.state': 1, population: -1} }, {$group: { _id: {state: '$_id.state'}, largestcity: {$first: '$_id.city'}, largestpopulation: {$first: '$population'}, smallestcity: {$last: '$_id.city'}, smallestpopulation: {$last: '$population'} } }, {$project: { _id: 0, state: '$_id.state', largest: { city: '$largestcity', population: '$largestpopulation' }, smallest: { city: '$smallestcity', population: '$smallestpopulation' } } }, {$sort: {state: 1} } ) -- Normalize the persons collection on languages db.persons.aggregate ( {$project: {_id: 0, name: {$concat: ['$name.first', ' ', '$name.last']}, languages: 1 } }, {$unwind: '$languages'} ) -- From persons collection extract {name, language, proficiency}, sorted by language name and person name db.persons.aggregate ( {$project: {_id: 0, name: {$concat: ['$name.first', ' ', '$name.last']}, languages: 1 } }, {$unwind: '$languages'}, {$sort: {'languages.name': 1, name: 1}}, {$project: {language: '$languages.name', name: 1, proficiency: '$languages.proficiency'} } ) -- Count the number of people at different proficiency levels in the above requirement db.persons.aggregate ( {$project: {_id: 0, name: {$concat: ['$name.first', ' ', '$name.last']}, languages: 1 } }, {$unwind: '$languages'}, {$sort: {'languages.name': 1, 'languages.proficiency': 1}}, {$group: {_id: {language: '$languages.name', proficiency: '$languages.proficiency'}, count: {$sum: 1}}}, {$project: {_id: 0, language: '$_id.language', proficiency: {level: '$_id.proficiency', count: '$count'}}}, {$group: {_id: '$language', proficiency: {$addToSet: '$proficiency'} } } ) ---------------------------------- - Transactions with two-phase commit ---------------------------------- db.accounts.insert ( {customer: 'Pradyumn', balance: 1000, pendingTransactions: []}) db.accounts.insert ( {customer: 'Preeti', balance: 2000, pendingTransactions: []}) db.accounts.find() db.transactions.insert ( {from: 'Pradyumn', to: 'Preeti', amount: 111, initiatedby: 'Thread1', state: 'initial'}) db.transactions.find() t = db.transactions.findOne ( {state: 'initial', initiatedby: 'Thread1'}) db.transactions.update( {_id: t._id}, {$set: {state: 'pending'}} ) db.transactions.find() db.accounts.update( {customer: t.from, pendingTransactions: {$ne: t._id}}, {$inc: {balance: -t.amount}, $push: {pendingTransactions: t._id}} ) db.accounts.update( {customer: t.to, pendingTransactions: {$ne: t._id}}, {$inc: {balance: t.amount}, $push: {pendingTransactions: t._id}} ) db.accounts.find() db.transactions.update( {_id: t._id}, {$set: {state: 'committed'}} ) db.transactions.find() db.accounts.update ( {customer: t.from}, {$pull: {pendingTransactions: t._id}} ) db.accounts.update ( {customer: t.to}, {$pull: {pendingTransactions: t._id}} ) db.accounts.find() db.transactions.update ( {_id: t._id}, {$set: {state: 'done'}} ) db.transactions.find() ---------------------------------- - Sharding, on one machine with different processes ---------------------------------- -- We’ll create two shards (shard1 and shard2) -- each shard will have three servers in a replica set, with the following port numbers and data folders: -- shard1a: 27021, c:\data\shard1\shard1a -- shard1b: 27022 , c:\data\shard1\shard1b -- shard1c: 27023 , c:\data\shard1\shard1c -- shard2a: 27031 , c:\data\shard2\shard2a -- shard2b: 27032 , c:\data\shard2\shard2b -- shard2c: 27033 , c:\data\shard2\shard2c -- We also need at least one (okay in development environment) or three (recommended for production environment) config -- servers: -- config1: 27041 -- config2: 27042 -- config3: 27043 -- In three separate command windows, give the following commands: mongod --shardsvr --replSet shard1 --dbpath c:\data\shard1\shard1a --logpath shard1a.log -port 27021 mongod --shardsvr --replSet shard1 --dbpath c:\data\shard1\shard1b --logpath shard1b.log -port 27022 mongod --shardsvr --replSet shard1 --dbpath c:\data\shard1\shard1c --logpath shard1c.log -port 27023 -- In one command window, give the following command: mongo --port 27021 -- and then config = {_id: 'shard1', members: [ {_id: 0, host: 'localhost:27021'}, {_id: 1, host: 'localhost:27022'}, {_id: 2, host: 'localhost:27023'}, ] } rs.initiate (config) rs.status() -- Similarly for the second shard: mongod --shardsvr --replSet shard2 --dbpath c:\data\shard2\shard2a --logpath shard2a.log -port 27031 mongod --shardsvr --replSet shard2 --dbpath c:\data\shard2\shard2b --logpath shard2b.log -port 27032 mongod --shardsvr --replSet shard2 --dbpath c:\data\shard2\shard2c --logpath shard2c.log -port 27033 mongo --port 27031 config = {_id: 'shard2', members: [ {_id: 0, host: 'localhost:27031'}, {_id: 1, host: 'localhost:27032'}, {_id: 2, host: 'localhost:27033'}, ] } rs.initiate (config) rs.status() -- Now for config servers -- directories: c:\data\config1, 2, 3 mongod --configsvr --dbpath c:\data\config1 --logpath config1.log --port 27041 mongod --configsvr --dbpath c:\data\config2 --logpath config2.log --port 27042 mongod --configsvr --dbpath c:\data\config3 --logpath config3.log --port 27043 -- Now for mongos (no port number given, so standard port) mongos --logpath mongos.log --configdb localhost:27041,localhost:27042,localhost:27043 -- Now connect mongo shell to mongos (no port number given, so standard port) mongo db.adminCommand ( {addshard: 'shard1/localhost:27021'} ) db.adminCommand ( {addshard: 'shard2/localhost:27031'} ) db.adminCommand ( {enableSharding: 'test'} ) db.adminCommand ( {shardCollection: 'test.persons', key: {name: 1} } ) sh.status() db.persons.stats() db.persons.find(...).explain() ---------------------------------- - Sharding, on physical machines ---------------------------------- -- shard1, on all three machines give the same command as follows: mongod --shardsvr --replSet shard1 -- shard2, on all three machines give the command as follows: mongod --shardsvr --replSet shard2 -- shard3 on all three machines give the command as follows: mongod --shardsvr --replSet shard3 -- In client machine: mongo 172.16.11.19:27018 -- and then config = {_id: 'shard1', members: [ {_id: 0, host: '172.16.11.19:27018'}, {_id: 1, host: '172.16.11.242:27018'}, {_id: 2, host: '172.16.11.184:27018'} ] } rs.initiate (config) rs.status() -- In another client window: mongo 172.16.11.250:27018 config = {_id: 'shard2', members: [ {_id: 0, host: '172.16.11.250:27018'}, {_id: 1, host: '172.16.11.244:27018'}, {_id: 2, host: '172.16.11.249:27018'}, {_id: 3, host: '172.16.11.23:27018'}, ] } rs.initiate (config) rs.status() -- Similarly in third client window for shard3 config = {_id: 'shard1', members: [ {_id: 0, host: '172.16.11.212:27018'}, {_id: 1, host: '172.16.11.246:27018'}, {_id: 2, host: '172.16.11.76:27018'} ] } rs.initiate (config) rs.status() -- To create config servers on 3 machines, on each of them run (these machines should have c:\data\configdb folder) mongod --configsvr mongod --configsvr mongod --configsvr -- Now for mongos (no port number given, so standard port) mongos --configdb 172.16.11.44:27019,172.16.11.177:27019,172.16.11.243:27019 -- Another client window, to connect to mongos (no port number given, so standard port) mongo 172.16.11.241 sh.addShard ('shard1/172.16.11.19:27018') sh.addShard ('shard1/172.16.11.250:27018') sh.addShard ('shard1/172.16.11.212:27018') -- or db.adminCommand ( {addshard: 'shard1/172.16.11.19:27018'} ) db.adminCommand ( {addshard: 'shard2/172.16.11.250:27018'} ) db.adminCommand ( {addshard: 'shard3/172.16.11.212:27018'} ) sh.shardCollection ('pragati.persons', {name: 1}, true) db.adminCommand ( {enableSharding: 'pragati'} ) db.adminCommand ( {shardCollection: 'pragati.persons', key: {name: 1} } ) sh.status() db.persons.stats() db.persons.find(...).explain() mongos> show collections actionlog changelog chunks collections databases employees lockpings locks mongos pradyumn.friends settings shards system.indexes tags version mongos> db.chunks.findOne() { "_id" : "pragati.persons-name_MinKey", "lastmod" : Timestamp(1, 0), "lastmodEpoch" : ObjectId("5565afd364a5abcad2436d40"), "ns" : "pragati.persons", "min" : { "name" : { "$minKey" : 1 } }, "max" : { "name" : { "$maxKey" : 1 } }, "shard" : "shard1" } mongos> db.collections.findOne() { "_id" : "pragati.persons", "lastmod" : ISODate("2015-05-27T11:51:47.326Z"), "dropped" : false, "key" : { "name" : 1 }, "unique" : false, "lastmodEpoch" : ObjectId("5565afd364a5abcad2436d40") } mongos> db.databases.find() { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "pragati", "partitioned" : true, "primary" : "shard1" } { "_id" : "pradyumn", "partitioned" : true, "primary" : "shard1" } mongos> db.mongos.find().pretty() { "_id" : "trg1525:27017", "ping" : ISODate("2015-06-01T08:42:15.221Z"), "up" : 168392, "waiting" : true, "mongoVersion" : "3.0.1" } { "_id" : "trg1984:27017", "ping" : ISODate("2015-05-28T12:33:38.106Z"), "up" : 1708, "waiting" : false, "mongoVersion" : "3.0.1" } mongos> db.settings.find().pretty() { "_id" : "chunksize", "value" : 64 } mongos> db.shards.find().pretty() { "_id" : "shard1", "host" : "shard1/172.16.11.184:27018,172.16.11.19:27018,172.16.11.242:27018" } { "_id" : "shard2", "host" : "shard2/172.16.11.23:27018,172.16.11.244:27018,172.16.11.249:27018,172.16.11.250:27018" } { "_id" : "shard3", "host" : "shard3/172.16.11.212:27018,172.16.11.246:27018,172.16.11.76:27018" } mongos> use admin switched to db admin mongos> db.runCommand ( {listShards: 1} ) { "shards" : [ { "_id" : "shard1", "host" : "shard1/172.16.11.184:27018,172.16.11.19:27018,172.16.11.242:27018" }, { "_id" : "shard2", "host" : "shard2/172.16.11.23:27018,172.16.11.244:27018,172.16.11.249:27018,172.16.11.250:27018" }, { "_id" : "shard3", "host" : "shard3/172.16.11.212:27018,172.16.11.246:27018,172.16.11.76:27018" } ], "ok" : 1 } mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5565ad8f64a5abcad2436ccb") } shards: { "_id" : "shard1", "host" : "shard1/172.16.11.184:27018,172.16.11.19:27018,172.16.11.242:27018" } { "_id" : "shard2", "host" : "shard2/172.16.11.23:27018,172.16.11.244:27018,172.16.11.249:27018,172.16.11.250:27018" } { "_id" : "shard3", "host" : "shard3/172.16.11.212:27018,172.16.11.246:27018,172.16.11.76:27018" } balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "pragati", "partitioned" : true, "primary" : "shard1" } pragati.employees shard key: { "name" : 1 } chunks: shard1 1 { "name" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0) pragati.persons shard key: { "name" : 1 } chunks: shard1 1 { "name" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0) { "_id" : "pradyumn", "partitioned" : true, "primary" : "shard1" } sh.stopBalancer () sh.getBalancerState() sh.startBalancer() sh.disableBalancing ('pradyumn.persons') sh.enableBalancing ('pradyumn.persons') ------ Removing a shard sh.status() use admin db.runCommand ( {removeShard: 'shard3'} ) mongos> use admin switched to db admin mongos> db.runCommand ( {removeShard: 'shard3'} ) { "msg" : "draining started successfully", "state" : "started", "shard" : "shard3", "ok" : 1 } mongos> db.runCommand ( {removeShard: 'shard3'} ) { "msg" : "removeshard completed successfully", "state" : "completed", "shard" : "shard3", "ok" : 1 } db.runCommand ( {movePrimary: 'pradyumn.zips', to: 'shard2'} ) ------ Changing the chunk size use config db.settings.save ( {_id: 'chunksize', value: 1}) -------------------------- - Configuration file -------------------------- -- Create a file c:\config\config.txt as follows: systemLog: destination: file path: "c:\\data\\log\\pragati.log" logAppend: false verbosity: 5 -- Start mongod as follows: mongod --config c:\data\config\config.txt -------------------------- - Authentication -------------------------- use admin db.createUser ( { user: 'pradyumn', pwd: 'mark', roles: [ {role: 'readWrite', db: 'pradyumn'}, {role: 'read', db: 'pragati'} ] } ) db.createUser ( { user: 'superuser', pwd: 'superpwd', roles: ['root'] }) db.createUser ( { user: 'santosh', pwd: 'gond', roles: [ {role: 'userAdminAnyDatabase', db: 'admin'} ] } ) db.createUser ( { user: 'mahendra', pwd: 'singh', roles: [ {role: 'userAdmin', db: 'pradyumn'} ] } ) mongod --config c:\data\config\config.txt --auth mongo -u superuser -p superpwd -authenticationDatabase admin mongo -u pradyumn -p mark -authenticationDatabase admin mongo -u santosh -p gond -authenticationDatabase admin db.createUser ( { user: 'roger', pwd: 'roger', roles: [ {role: 'read', db: 'pradyumn'} ] } ) db.dropUser ('pradyumn') use admin db.createRole ( { role: 'hrExecutive', roles: [ {role: 'read', db: 'pradyumn'}, {role: 'read', db: 'pragati'} ], privileges: [ {resource: {db: 'pradyumn', collection: 'zips'}, actions: ['insert', 'remove']}, {resource: {db: '', collection: ''}, actions: ['changeOwnPassword']} ] } ) db.createUser ( { user: 'preeti', pwd: 'mark', roles: [ {role: 'hrExecutive', db: 'admin'} ] } ) mongo -u preeti -p mark -authenticationDatabase admin db.updateUser ( 'preeti', {pwd: 'changeit'} ) -- fails as right not granted to pradyumn mongo -u pradyumn -p mark -authenticationDatabase admin db.updateUser ( 'pradyumn', {pwd: 'changeit'} ) mongo -u superuser -p superpwd -authenticationDatabase admin use admin db.grantRolesToUser ( 'pradyumn', [ {role: 'hrExecutive', db: 'admin'} ] ) -- Identify a user's roles db.getUser ('pradyumn') --output: > db.getUser ('pradyumn') { "_id" : "admin.pradyumn", "user" : "pradyumn", "db" : "admin", "roles" : [ { "role" : "readWrite", "db" : "pradyumn" }, { "role" : "read", "db" : "pragati" }, { "role" : "hrExecutive", "db" : "admin" } ] } -- Identify privileges for a role use admin db.getRole ('hrExecutive', {showPrivileges: true}) -- Revoke a role from a user db.revokeRolesFromUser ( 'pradyumn', [ {role: 'hrExecutive', db: 'admin'} ] ) -- View a role in current database db.runCommand ( {rolesInfo: 'hrExecutive'} ) --Output > db.runCommand ( {rolesInfo: 'hrExecutive'} ) { "roles" : [ { "role" : "hrExecutive", "db" : "admin", "isBuiltin" : false, "roles" : [ { "role" : "read", "db" : "pradyumn" }, { "role" : "read", "db" : "pragati" } ], "inheritedRoles" : [ { "role" : "read", "db" : "pradyumn" }, { "role" : "read", "db" : "pragati" } ] } ], "ok" : 1 } -- View a role in a different database use pradyumn db.runCommand ( {rolesInfo: {role: 'hrExecutive', db: 'admin'} } ) -- View all custom roles use admin db.system.roles.find() -- Change a user's password db.changeUserPassword ('pradyumn', 'changeit') -------------------------- - Write Concern -------------------------- db.persons.insert ( { name: {first: 'Salil', last: 'Gupta'}, gender: 'M', yearOfBirth: 1960, livesIn: 'Mumbai', counriesVisited: ['India', 'United States of America', 'Netherlands', 'China'], languages: [ {name: 'English', proficiency: 'Fluent'}, {name: 'Hindi', proficiency: 'Intermediate'}], married: 'Yes' }, {writeConcern: {w: 'majority', wtimeout: 5000}} ) -------------------------- - Profiler -------------------------- mongod --profile 1 --slowms 2 mongo db.scores.find({student_id: 1}) db.system.profile.find().pretty() db.system.profile.find( {ns: 'pragati.scores'}).sort ({millis: -1}).pretty() db.getProfilingLevel() db.getProfilingStatus() db.setProfilingLevel (1, 10) db.setProfilingLevel (0) ----------------------- - Sundry, to add in PPT ----------------------- db.stats() -- Running a command on another database use test db.getSiblingDB ("pragati").persons.count() -- Compact space db.adminCommand ({repairDatabase: 1}) ----------------------------- - Simulating auto-increment ----------------------------- db.counters.insert ( {_id: 'employeeid', seq: 0} ) function getNextSequence(name) { var ret = db.counters.findAndModify( { query: { _id: name }, update: { $inc: { seq: 1 } }, new: true } ); return ret.seq; } db.employees.insert ( {_id: getNextSequence ('employeeid'), name: 'Bhagyarekha Sonar'} ) db.employees.insert ( {_id: getNextSequence ('employeeid'), name: 'Sophia Andrade'} ) db.employees.insert ( {_id: getNextSequence ('employeeid'), name: 'Gopal Mirani'} ) --------------------------- - Mongodump --------------------------- mongodump --help mongodump /h 172.16.11.241 /d pradyumn /c zips /o dumpoutputs mongodump /h 172.16.11.241 /d pradyumn /o dumpoutputs mongodump /h 172.16.11.241 /o dumpoutputs mongorestore /h 172.16.11.241 /d pradyumn /c zips /dir: dumpoutputs\pradyumn\zips.bson mongorestore /h 172.16.11.241 /d pradyumn /dir: dumpoutputs\pradyumn mongorestore /h 172.16.11.241 /d pradyumn /dir: dumpoutputs\pradyumn /drop ---------------------- - Users ---------------------- db.createUser ( {user: 'pradyumn', pwd: 'idontknow', roles: ['readWrite', 'dbAdmin'] } ) -------------------- - Homework 4.3 -------------------- db.posts.ensureIndex ({date: -1}) db.posts.ensureIndex ({tags: 1, date: -1}) db.posts.ensureIndex ({permalink:1}) -------------------- - Homework 4.4 -------------------- db.profile.find ( {"ns": "school2.students"}).sort( {millis: -1}).limit(1).pretty() -------------------- - Homework 5.1 -------------------- db.posts.aggregate ( {$project: {_id:1, comments: 1} }, {$unwind: '$comments'}, {$group: {_id: '$comments.author', comms: {$sum: 1} } }, {$sort: {comms: 1}} ) ---------------------- - Homework 5.2 ---------------------- db.zips.aggregate ( {$match: {state: {$in: ['CA', 'NY'] } } }, {$group: {_id: {state: '$state', city: '$city'}, population: {$sum: '$pop'} } }, {$match: {population: {$gt: 25000} } }, {$group: {_id: 1, avgpop: {$avg: '$population'} } } ) ---------------------- - Homework 5.3 ---------------------- -- Steps -- 1. Denormalize scores with $unwind -- 2. Drop quiz scores with $match -- 3. Get average marks for class-student combination with $group -- 4. Get average marks for class with $group -- 5. Sort in descending order of average marks with $sort -- 6. (Optional) limit the output to 1 document with $limit db.grades.aggregate ( {$unwind: '$scores'}, {$match: {'scores.type' : {$in: ['exam', 'homework'] } } }, {$group: {_id: {class_id: '$class_id', student_id: '$student_id'}, average_marks: {$avg: '$scores.score'} } }, {$group: {_id: {class_id: '$_id.class_id'}, average_marks: {$avg: '$average_marks'} } }, {$sort: {average_marks: -1} }, {$limit: 1} ) ---------------------- - Homework 5.4 ---------------------- db.zips.aggregate ( {$project: {_id: 1, firstchar: {$substr: ['$city', 0, 1] }, pop: 1} }, {$match: {firstchar: {$in: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] } } }, {$group: {_id: 1, ruralpopulation: {$sum: '$pop'} } } ) ---------------------- - Final Exam 1 ---------------------- db.messages.find ( {'headers.From': 'andrew.fastow@enron.com', 'headers.To': 'jeff.skilling@enron.com'} ).count() ---------------------- - Final Exam 2 ---------------------- db.messages.aggregate ( {$project: {from: '$headers.From', to: '$headers.To'} }, {$unwind: '$to'}, {$group: {_id: {_id: '$_id', from: '$from'}, to: {$addToSet: '$to'} } }, {$unwind: '$to'}, {$group: {_id: {from: '$_id.from', to: '$to'}, mail_count: {$sum: 1} } }, {$sort: {mail_count: -1} } )