MongoDB Tutorial
Download
Report
Transcript MongoDB Tutorial
Ayush Mishra ([email protected])
Installation
Configuration
Querying
PHP – Mongo Link and setup
Gentle introduction to ETL (MySQL to MongoDB)
MONGO DB
Install tutorial
https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
Download Mongo DB here
https://www.mongodb.org/downloads?_ga=1.114949455.91923056.1453301539#p
roduction
MongoDB is an open-source document database and leading NoSQL database
MongoDB tends to create and deploy a highly scalable and performance-oriented
database.
RDBMS
MongoDB
Database
Database
Table
Collection
Tuple/Row
Document
column
Field
Table Join
Embedded Documents
Primary Key
Primary Key (Default key _id provided by
mongodb itself)
open command prompt
>>wmic os get osarchitecture
OSArchitecture
64-bit
Create data directory for Mongo
>>md \data\db
cd into bin and start server with the connection to the data dir
>>cd c:\program files\mongodb\server\3.4\bin
>>mongod.exe --dbpath c:\data\db
then open another command prompt and start mongo DB shell to issue commands
>>cd c:\program files\mongodb\server\3.4\bin
>>mongo
tell mongo which database to use
>> use db sports
create a new collection
>> db.createCollection("footballTeams")
list existing collections
> show collections
footballTeams
Insert documents into collections
> db.footballTeams.insert({"name":"Steelers","city":"Pittsburgh"})
WriteResult({ "nInserted" : 1 })
> db.footballTeams.insert({"name":"Jets","city":"New York"})
WriteResult({ "nInserted" : 1 })
> db.footballTeams.insert({"name":"Patriots","city":"New England"})
WriteResult({ "nInserted" : 1 })
> db.footballTeams.insert({"name":"Broncos","city":"Denver"})
WriteResult({ "nInserted" : 1 })
QUERY TO FIND A SPECIFIC DOCUMENT
> db.footballTeams.find({"name":"Steelers"})
{ "_id" : ObjectId("56a3b13a671657521fbd4220"), "name" : "Steelers", "city" : "P
ittsburgh" }
> db.footballTeams.find({"city":"New York"})
{ "_id" : ObjectId("56a3b1c7671657521fbd4221"), "name" : "Jets", "city" : "New Y
ork" }
To display the results in a formatted way, you can use pretty() method.
> db.footballTeams.find({"city":"New York"}).pretty()
LIKE QUERY example
> db.footballTeams.find({"city":/New/})
{ "_id" : ObjectId("56a3b1c7671657521fbd4221"), "name" : "Jets", "city" : "New Y
ork" }
{ "_id" : ObjectId("56a3b1d6671657521fbd4222"), "name" : "Patriots", "city" : "N
ew England" }
USE of AND/OR while Querying:
>db.footballTeams.find({$and:[{“city”:/New/},{“name”:”Jets”}]}).pretty()
>db.footballTeams.find({$or:[{“city”:/New/},{“name”:”Jets”}]}).pretty()
AND/OR together
> db.footballTeams.find({“city”:/New/,$or:[{“name”:”Jets”},{“name”:”Patriots”}]}).pretty()
Use $set
>db.footballTeams.update({'city':'Pittsburgh' },{$set:{'city':'Pgh'}})
>db.footballTeams.remove({'name':'Broncos'})
Retrieve only select fields instead of returning all data
>db.footballTeams.find({},{"city":1,”name”:1,_id:0})
>db.footballTeams.find({},{"city":1,_id:0}).limit(2)
Sort descending order
>db.footballTeams.find({},{"city":1,_id:0}).sort({"city":-1})
> db.footballTeams.aggregate([{$group : {_id : "$city", num_teams : {$sum : 1}}}])
-
Concept of Pipelining
-
Example:
db.movies.aggregate([{$unwind:"$genres"},{$group:{"_id":"$genres",count:{$sum:
1}}},{$sort:{count:-1}},{$limit:1}])
Query: Find the number of unique users who rated the top 5 most popular movie
tags
db.tags.aggregate([{$group:{_id:"$tag",count:{$sum:1}}},{$sort:{count:-1}},
{$limit:5},{$out:"topTags"}])
var topTagIds = new Array()
db.topTags.find({},{_id:1}).forEach(function(doc){topTagIds.push(doc._id)})
db.tags.distinct("userId",{"tag":{$in:topTagIds}}).length
Bulk Load of data into MongoDB (Hint: Python is the way to go…)
Run aggregation queries
https://www.dropbox.com/sh/8a0heam9o9qeloc/AACQw1c4mE_t8gwcwdWPO9z
ma?dl=0
Link: https://s3.amazonaws.com/drivers.mongodb.org/php/index.html
“C:\wamp64\bin\php\php5.6.25\ext”
Modify php.ini; include “extension = php_mongo.dll”
We can extract data from sql
Need to transform it into a document collection format that is required
Load it into the mongoDB
It doesn’t get any simpler!!!!