/** * This is modified from: * https://docs.mongodb.com/getting-started/shell/ * https://docs.mongodb.com/getting-started/shell/introduction/ * https://www.mongodb.com/mongodb-architecture * See also: * For Node: https://mongodb.github.io/node-mongodb-native/ * * 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 javascript shell, but we have to connect remotely... // DO THE FOLLOWING IN A SHELL FROM CSLINUX!!! mongo --host mcsdb.utm.utoronto.ca -u $UTORID -p --authenticationDatabase $UTORID_309 --shell // When prompted: The password being the last 5 digits before the last 3 digits of the student barcode. // or all in one line... // DO THE FOLLOWING IN A SHELL FROM CSLINUX!!! mongo --host mcsdb.utm.utoronto.ca -u "$UTORID" -p "PASSWORD_AS_ABOVE" --authenticationDatabase $UTORID_309 --shell // https://docs.mongodb.com/manual/reference/mongo-shell/ help /** * https://docs.mongodb.com/getting-started/shell/import-data/ * Download https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json * DO THE FOLLOWING IN A SHELL FROM CSLINUX!!! **/ wget https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json mongoimport --host mcsdb.utm.utoronto.ca -u "$UTORID" -p "PASSWORD_AS_ABOVE" --db $UTORID_309 --collection restaurants --drop --file primer-dataset.json /** * now in the mongo shell (the one you launched above)... **/ show dbs // You can't do this! You aren't allowed!! use $UTORID_309 // switch to your database show collections // Collections = documents of similar 'type' , if you imported properly, you should see the restaurants collection // 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().pretty() // so you can read it! 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(); var f = db.restaurants.find(); // suppress output f[0]; f[1]; t = []; for(var i = 0;i