MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB...

13
MongoDB Crash Course ComputeFest Niv Dayan 12 January, 2017

Transcript of MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB...

Page 1: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

MongoDB CrashCourse

ComputeFest

Niv Dayan

12 January, 2017

Page 2: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

Installation

OSX: https://www.youtube.com/watch?v=G--uGbOFF9E(youmayneedtoadd“sudo”asaprefixtoeveryinstallationcommand)

Windows: https://www.youtube.com/watch?v=K_5mj3-_uJQ

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 3: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

StartingMongoDB

CommandstorunareinblueStartserver.Openterminalwindowandrun:

mkdir DBmongod --dbpath DB

Startclient.Opendifferentterminalandrun:mongo

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 4: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

Lookslikethis:

Serverwindow Clientwindow

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 5: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

CreateaCollection(inclient)

Createadatabaseentryvar customer={name:"bob",balance:100}db.customers.insert(customer)

Wenowhaveonecollectionofcustomersshowcollections

Inthecollection,wehaveonecustomerdb.customers.find()

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 6: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

PrettyPrinting

Printthingsnicelydb.customers.find().pretty()

NotethateachdocumentwithinacollectionhasauniqueIDthatiscreatedautomatically

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 7: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

Filtering

Createanothercustomervar customer2={name:"sara",balance:200}db.customers.insert(customer2)

Again,listallcustomers:db.customers.find().pretty()

ListcustomerscalledSaradb.customers.find({name:"sara"}).pretty()

Listcustomerswithbalancegreaterthan150db.customers.find({balance:{$gt:150}}).pretty()

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 8: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

UpdatingDocuments

Givebobasurnamedb.customers.update(

{name:"bob"},{$set:{surname:"Dylan"}})Add50toBob’saccount

db.customers.update({name:"bob"},{$inc :{balance:50}})

Checkoutcomedb.customers.find().pretty()

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 9: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

CopyingaDocument

CreateacopyofBobvar cust =db.customers.findOne({name:"bob"})

Printthesurnameofthecopycust["surname"]

Changethesurnameofthecopycust["surname"]="Marley"

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 10: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

SavingaCopytotheDatabase

Trytosavethecopyinthedatabase(won’twork)db.customers.insert(cust)

GenerateanewobjectIDvar o=ObjectId()

AssignthenewIDtothecopycust["_id"]=o

Nowtrytoinsertagaindb.customers.insert(cust)

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 11: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

Aggregation

Findsumofallaccountbalancesdb.customers.aggregate([{$group:{

_id:null,total:{$sum:"$balance"}}}])

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 12: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

Shutdownserver

Shutdowntheserverandquitclientuseadmindb.shutdownServer()exit

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE

Page 13: MongoDB Crash Course - Niv Dayan - GitHub Pagesnivdayan.github.io/mongo.pdf · Starting MongoDB Commands to run are in blue Start server. Open terminal window and run: mkdirDB mongod--dbpathDB

Thanks!

13

INSTITUTEFORAPPLIEDCOMPUTATIONALSCIENCE