https://docs.mongodb.com/getting-started/shell/ https://docs.mongodb.com/getting-started/shell/introduction/ https://www.mongodb.com/mongodb-architecture A Document { "_id" : ObjectId("54c955492b7c8eb21818bd09"), "address" : { "street" : "2 Avenue", "zipcode" : "10075", "building" : "1480", "coord" : [ -73.9557413, 40.7720266 ] }, "borough" : "Manhattan", "cuisine" : "Italian", "grades" : [ { "date" : ISODate("2014-10-01T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-01-16T00:00:00Z"), "grade" : "B", "score" : 17 } ], "name" : "Vella", "restaurant_id" : "41704620" } mongo # the mongo shell # https://docs.mongodb.com/manual/reference/mongo-shell/ help # https://docs.mongodb.com/getting-started/shell/import-data/ mongoimport --db test --collection restaurants --drop --file primer-dataset.json mongo show dbs use test show collections // Collections = documents of similar 'type' // CREATE db.restaurants.insert( { "address" : { "street" : "2 Avenue", "zipcode" : "10075", "building" : "1480", "coord" : [ -73.9557413, 40.7720266 ] }, "borough" : "Manhattan", "cuisine" : "Italian", "grades" : [ { "date" : ISODate("2014-10-01T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-01-16T00:00:00Z"), "grade" : "B", "score" : 17 } ], "name" : "AVella", "restaurant_id" : "41704620" } ) // RETRIEVE db.restaurants.find() db.restaurants.find( { "borough": "Manhattan" } ) // exact match on top level field db.restaurants.find( { "address.zipcode": "10075" } ) // embedded document db.restaurants.find( { "grades.grade": "B" } ) // array match // can chain commands db.restaurants.find( { "borough": "Manhattan" } ).count(); // other operators $gt, $lt, $eq, $gte, ... // https://docs.mongodb.com/manual/reference/operator/query-comparison/ db.restaurants.find( { "borough" : { $eq : "Manhattan" } } ) // exact match on top level db.restaurants.find( { "grades.score": { $gt: 30 } } ) db.restaurants.find( { "grades.score": { $lt: 10 } } ) // AND conditions db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } ) db.restaurants.find( { $and: [ {"cuisine": "Italian"}, {"address.zipcode": "10075" }] } ) // OR conditions db.restaurants.find( { $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] } ) // sort results db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } ) // 1 = increasing, -1 = decreasing // UPDATE // Updating data: Can't modify _id // Change one existing record ($set its fields, new or otherwise) db.restaurants.update( { "name" : "Juni" }, { $set: { "cuisine": "American (New)" }, $currentDate: { "lastModified": true } } ) // embedded field db.restaurants.update( { "restaurant_id" : "41156888" }, { $set: { "address.street": "East 31st Street" } } ) // update multiple documents {multi : true} db.restaurants.update( { "address.zipcode": "10016", cuisine: "Other" }, { $set: { cuisine: "Category To Be Determined" }, $currentDate: { "lastModified": true } }, { multi: true} ) // Replace a whole document: Notice no $set db.restaurants.update( { "restaurant_id" : "41704620" }, { "name" : "Vella 2", "address" : { "coord" : [ -73.9557413, 40.7720266 ], "building" : "1480", "street" : "2 Avenue", "zipcode" : "10075" } } ) // DELETE db.restaurants.remove( { "borough": "Manhattan" } ) // remove all matching criteria db.restaurants.remove( { "borough": "Queens" }, { justOne: true } ) db.restaurants.remove( { } ) // leaves the collection empty db.restaurants.drop() // remove the collection // AGGREGATION db.collection.aggregate( [ , , ... ] ) // stages are sequences of aggregate operations to perform db.restaurants.aggregate( [ { $group: { "_id": "$borough", "count": { $sum: 1 } } } ] ); db.restaurants.aggregate( [ { $match: { "borough": "Queens", "cuisine": "Brazilian" } }, { $group: { "_id": "$address.zipcode" , "count": { $sum: 1 } } } ] db.restaurants.aggregate( [ { $match: { "borough": "Queens", "cuisine": "Brazilian" } }, { $group: { "_id": "$address.zipcode" , "average": { $avg: { $avg : "$grades.score"} } } } ] ); f = db.restaurants.find(); f[0]; f[1]; t = []; for(var i = 0;i