9b. document-oriented databases lab

28
Document Oriented Databases Lab Ciao ciao Vai a fare ciao ciao Dr. Fabio Fumarola

Upload: fabio-fumarola

Post on 06-Aug-2015

591 views

Category:

Data & Analytics


1 download

TRANSCRIPT

Document OrientedDatabases

LabCiaociao

Vai a fare

ciao ciaoDr. Fabio Fumarola

MongoDB

2

MongoDB with Docker• Download docker images:

– https://registry.hub.docker.com/_/mongo/

• Run Mongo:– docker run --name mongo -d -p 27017:27017 mongo

• Connect to Mongo:– docker exec -it mongo bash

• Connect via robomongo:– http://robomongo.org/

3

Mongodb Fundamentals

4

Starting Mongo• Asking for help:

– help

5

Import same data• curl -L http://j.mp/OSCONvenues | mongoimport -d

conf -c venues

6

Example of object

7

var place1 = { "name" : "10gen HQ", "address" : "578 Broadway 7th Floor", "city" : "New York", "zip" : "10011", "tags" : [ "business", "awesome" ]

}

Insert>use places

>db.places.insert(place1)

It returns: WriteResult({ "nInserted" : 1 })

8

Querying> db.places.find()

•Specify equality conditions> db.places.find({ zip: "10011", tags: "awesome" })

9

{ "_id" : ObjectId("555473fb37abe83242ddd405"), "name" : "10gen HQ", "address" : "578 Broadway 7th Floor", "city" : "New York", "zip" : "10011", "tags" : [ "business", "awesome" ]

}

Nested Documents

10

var place2 = {"name" : "10gen HQ","address" : "578 Broadway 7th Floor","city" : "New York","zip" : "10011","tags" : [

"business","awesome"

],"comments" : [

{ "date" : ISODate("2014-10-01T00:00:00Z"), "author" : "Fabio", "text" : "best place" } ]}

Querying Nested Documents• Find by specifying author

– db.places.find({"comments.author" : "Fabio"})

• Greater Than Operator ($gt):– db.places.find({"comments.score" : { $gt: 20}})

• Less Than Operator ($lt)– db.places.find({"comments.score" : { $lt: 20}})– db.places.find({"comments.score" : { $lt: 40}})

• Conditions can be logically combined

11

Logical Combination• Logical AND

– db.places.find({ "name" : "10gen HQ", "comments.author" : "Fabio" })– db.places.findOne({ "name" : "10gen HQ", "comments.author" :

"Fabio" })

• Logical OR– db.places.find({ $or : [{ "name" : "10gen HQ" }, {"comments.author" :

"Fabio" }]})

12

Sort• to sort by and the corresponding sort type:

– e.g. 1 for ascending – and -1 for descending.

• Example:– db.places.find().sort({"name" : 1, "comments.score" : -1})

13

Updating• We can use the update() method to update

documents of a collection. The method accepts as its parameters:– a filter document to match the documents to update,– an update document to specify the modification to

perform, and– an options parameter (optional).

14

Update

15

db.places.update( {name : "10gen HQ"}, { $push : { comments : { author : "steve", date : 6/26/2015, text : "Office hours are great!" } } })

Update Multi

16

db.places.update( {name : "10gen HQ"}, { $push : { comments : { author : "steve", date : 6/26/2015, text : "Office hours are great!" } } }, { multi: true})

Remove• We can use the remove() method to remove

documents from a collection. • The method takes a conditions document that

determines the documents to remove.• To specify a remove condition,

17

Remove Example

db.places.remove({"name" : "10gen HQ"})

db.places.remove({"name" : "10gen HQ"}, { justOne: true } )

18

Drop Collections and DB• The drop() method to drop a collection, including any

indexes– db.places.drop()

• Drop a db– use test– db.dropDatabase()

19

Advanced Operations

20

Regular Expression

21

// Regular Expressions

> db.posts.find({'comments.author': /^Fa/}){ _id :

ObjectId("4c4ba5c0672c685e5e8aabf3"),

name : "10gen HQ", address : "578 Broadway 7th Floor", city :

"New York",

zip : "10011",

comments : [ {

author : ”Fabio",

date : "Sat Apr 25 2010 20:51:03",

text : "Best Place Ever!"

} ]}

Cursor• How to get a cursor:

– var it = db.venues.find()

• Methods– next– hasNext

22

Aggregation• Group Documents by a Field and Calculate Count

23

db.venues.aggregate( [ { $group: { "_id": "$location.city", "count": { $sum: 1 } } } ])

Indexes• By default index on collections are only on the _id• the createIndex() method is used to create an index on a

collection• Indexes can support the efficient execution of queries.

– For an ascending index type, specify 1 for <type>.– For a descending index type, specify -1 for <type>.

24

Create Index

25

{"createdCollectionAutomatically" :

false,"numIndexesBefore" : 1,"numIndexesAfter" : 2,"ok" : 1

}

db.venues.createIndex({"country": 1})

Create a Compound Index

26

db.venues.createIndex({"country": 1, "distance": -1})

{"createdCollectionAutomatically" :

false,"numIndexesBefore" : 2,"numIndexesAfter" : 3,"ok" : 1

}

Upon successful index creation, the "numIndexesAfter" value is one greater than the "numIndexesBefore" value.

Geospatial Index

27

db.venues.ensureIndex({ "location.geo": "2d" })

db.venues.find({"location.geo":{$near:[-122.6600248120735,45.5302342677652]}})

db.venues.find( { "location.geo" : { $geoWithin : { $box : [ [ 0 , 0 ] , [ -122 , 48 ] ] } } } )

References• http://docs.mongodb.org/manual/• https://gist.github.com/df238517f06500fec317.git

28