Uploaded by Harsh Rathi

MongoDB

advertisement
MongoDB
BITS Pilani
Hyderabad
Campus
Pilani Campus
MongoDB
•
•
•
•
Properties
Installation
CRUD basics
Hands on
Pilani Campus
BITS Pilani, Hyderabad
Campus
Motivation
• Arguably Most famous or at least fastest emerging database for
big data and other applications
BITS Pilani, Hyderabad Campus
MongoDB
MongoDB: A popular open-source DBMS
- A document-oriented database as opposed to a relational database
Relational database:
Document-oriented DB:
Name School
Employer Occupation
Lori
Self
Entrepreneur
null
null
null
Malia Harvard
Relational databases have fixed
schemas; document-oriented
databases have flexible schemas
{
name: "Lori",
employer: "Self",
occupation: "Entrepreneur"
}
{
name: "Malia",
school: "Harvard"
}
BITS Pilani, Hyderabad Campus
MongoDB concepts
Database:
- A container of MongoDB collections
Collection:
- A group of MongoDB documents.
- (Table in a relational database)
Document:
- A JSON-like object that represents one instance of a collection
(Row in a relational database)
- Also used more generally to refer to any set of key-value pairs.
BITS Pilani, Hyderabad Campus
MongoDB Example
Seller:
• Store lots and lots of data and work with it efficiently
How it works:
• Database--shop
• Collections-users, orders
• Documents-name:”Amit”, age:30
•
name:”Manu”, age:45
• Schema less—Flexible
• Some kind of structure: JSON (data stored in BSON)
• Key –value pair
Pilani Campus
BITS Pilani, Hyderabad
Campus
Flights.json
[
{
"departureAirport": "MUC",
"arrivalAirport": "SFO",
"aircraft": "Airbus A380",
"distance": 12000,
"intercontinental": true
},
{
"departureAirport": "LHR",
"arrivalAirport": "TXL",
"aircraft": "Airbus A320",
"distance": 950,
"intercontinental": false
}
]
BITS Pilani, Hyderabad Campus
MongoDB example
Documents:
{ "_id" :
ObjectId("5922b8a186ebd7
3e42b1b53c"), "style" :
"july4", "message" :
"Dear Chip,\n\nHappy 4th
of July!\n\n❤️Dale" }
Database:
ecards-db
Collection:
card
The document keys are
called fields
{ "_id" :
ObjectId("5922acf09e7640
3b3a7549ec"), "style" :
"graduation", "message"
: "Hi Pooh,\n\n🎉
Congrats!!! 🎉\n\n<3
Piglet" }
{ "_id" :
ObjectId("5922b90d86ebd7
3e42b1b53d"), "style" :
"fathersday", "message"
: "HFD" }
BITS Pilani, Hyderabad Campus
Key properties
•
•
•
•
Allow complex relations to be stored in data
No Joins, fewer dependencies
MongoDB stored data in BSON for faster retrieval
Data stored in logical way
• Opposite of SQL
• Instead of Normalization (store data in different tables)
• Store in same collection
• Fewer relations, less tables
• Instead of fetching from Table A to B to C and get data
• Get it all at one place: Speed and flexibility
BITS Pilani, Hyderabad Campus
MongoDB Ecosystem
BITS Pilani, Hyderabad Campus
MongoDB installation
BITS Pilani, Hyderabad Campus
MongoDB Installation: Windows
•
•
•
•
•
•
•
•
Mongodb.com
Try free
Server
Download
Install (custom)🡪 MongoDB as a service
cmd🡪 mongo (error)
Set environment path
cmd 🡪 mongo (should work)
• In MAC and Linux have to run mongod (i.e., server separately)
BITS Pilani, Hyderabad Campus
Working with MongoDB
BITS Pilani, Hyderabad Campus
MongoDB is a software program running on the
computer, alongside the NodeJS server program.
It is also known as the MongoDB server.
There are MongoDB libraries we can use in NodeJS to
communicate with the MongoDB Server, which reads and
writes data in the database it manages.
Data
The database the MongoDB Server manages might be
local to the server computer...
(Routing,
etc…)
Or it could be stored on other server computer(s)
("cloud storage").
CRUD Basics
BITS Pilani, Hyderabad Campus
JSON and BSON
BITS Pilani, Hyderabad Campus
Operations on MongoDB
BITS Pilani, Hyderabad Campus
CRUD basics
BITS Pilani, Hyderabad Campus
mongo shell commands
> show dbs
- Displays the databases on the MongoDB server
> use databaseName
- Switches current database to databaseName
- The databaseName does not have to exist already
-
It will be created the first time you write data to it
> show collections
- Displays the collections for the current database
BITS Pilani, Hyderabad Campus
mongo shell commands
> db.collection
- Variable referring to the collection collection
> db.collection.find(query)
- Prints the results of collection matching the query
- The query is a MongoDB Document (i.e. a JSON object)
-
To get everything in the collection use db.collection.find()
To get everything in the collection that matches x=foo,
db.collection.find({x: 'foo'})
BITS Pilani, Hyderabad Campus
mongo shell commands
> db.collection.findOne(query)
- Prints the first result of collection matching the query
> db.collection.insertOne(document)
- Adds document to the collection
- document can have any structure
> db.test.insertOne({ name: 'dan' })
> db.test.find()
{ "_id" : ObjectId("5922c0463fa5b27818795950"), "name" : "dan"
}
MongoDB will automatically add a unique _id to every document in a
collection.
BITS Pilani, Hyderabad Campus
mongo shell commands
> db.collection.deleteOne(query)
- Deletes the first result of collection matching the query
> db.collection.deleteMany(query)
- Delete multiple documents from collection.
- To delete all documents, db.collection.deleteMany()
> db.collection.drop()
- Removes the collection from the database
BITS Pilani, Hyderabad Campus
mongo shell
When should you use the mongo shell?
- Adding test data
- Deleting test data
BITS Pilani, Hyderabad Campus
Time for practical Hands On
BITS Pilani, Hyderabad Campus
⮚show dbs
⮚use flights
⮚show dbs
>db.flightData.insertOne({"departureAirport": "MUC",
"arrivalAirport": "SFO",
"aircraft": "Airbus A380",
"distance": 12000,
"intercontinental": true
})
>db.flightData.find().pretty()
BITS Pilani, Hyderabad Campus
Flight Data
BITS Pilani, Hyderabad Campus
insert
>db.flightData.InsertOne({ "departureAirport": "LHR",
"arrivalAirport": "TXL",
"aircraft": "Airbus A320",
"distance": 950,
"intercontinental": false })
>db.flightData.InsertOne({"departureAirport": "LHR",
"arrivalAirport": "TXL“})
>db.flightData.InsertOne({"departureAirport": "LHR",
"arrivalAirport": "TXL“, “_id”:”lhr-txl-1”})
>db.flightData.find().pretty()
>cls
>db.flightData.deleteOne({departuteAirport:”LHR”})
>db.flightData.find().pretty()
BITS Pilani, Hyderabad Campus
delete and update
>db.flightData.deleteOne({_id:”lhr-txl-1”})
>db.flightData.find().pretty()
>db.flightData.deleteMany()
//error
>db.flightData.UpdateOne({distance:12000},{marker:”delete”}) //error
>db.flightData.UpdateOne({distance:12000},{$set: {marker:”delete”}})
>db.flightData.find().pretty()
>db.flightData.updateMany({ },{$set: {marker:”to delete”}})
>db.flightData.find().pretty()
>db.flightData.deleteMany({marker:”to delete”})
>db.flightData.find().pretty()
BITS Pilani, Hyderabad Campus
insert
>db.flightData.insertMany([
{
"departureAirport": "MUC",
"arrivalAirport": "SFO",
"aircraft": "Airbus A380",
"distance": 12000,
"intercontinental": true
},
{
"departureAirport": "LHR",
"arrivalAirport": "TXL",
"aircraft": "Airbus A320",
"distance": 950,
"intercontinental": false
}
])
>db.flightData.find().pretty()
BITS Pilani, Hyderabad Campus
find and update
>db.flightData.find("aircraft": "Airbus A380“)
>db.flightData.find("intercontinental": true).pretty()
>db.flightData.find(distance:12000)
>db.flightData.find({distance: {$gt:10000}})
>db.flightData.find({distance: {$gt:900}})
>db.flightData.findOne({distance: {$gt:900}})
>db.flightData.find().pretty()
>db.flightData.updateOne(_id:……………….,{$set:{delayed:true}})
>db.flightData.update(_id:……………….,{$set:{delayed:false}})
>db.flightData.updateOne(_id:……………….,{delayed:true}})
>db.flightData.updateMany(_id:……………….,{delayed:false}}) //error
>db.flightData.update(_id:……………….,{delayed:true}})
//outdated
>db.flightData.replaceOne(_id:……………….,{delayed:true}})
//overwrite
//error
BITS Pilani, Hyderabad Campus
Insert and find (cursor object)
>db.passengers.insertMany([
{
"name": "Max Schwarzmueller",
"age": 29
},
{
"name": "Manu Lorenz",
"age": 30
},
{
"name": "Chris Hayton",
"age": 35
},
{
"name": "Sandeep Kumar",
"age": 28
}])
>db.passengers.find().pretty()
>it
BITS Pilani, Hyderabad Campus
projection and find
>db.passengers.find({ }, {name: 1}).pretty() //id is always included: default
>db.passengers.find({ }, {name:1, _id:0}).pretty()
> db.passengers.find({ }, {name:1, _id:0, age:0}).pretty() //same as previous
All this happens at the server. No extra burden on network
>db.flightData.updateMany({ }, {$set :{status: {duration: “full”, runtime:”limited”}}})
>db.flightData.find().pretty()
>db.passengers.updateOne({name: “Armin Glutch”}, {$set: {hobbies: [“sports”,
“cooking”]}})
>db.passengers.find().pretty()
>db.passengers.find({name : “Armin Glutch”}).pretty()
>db.passengers.find({name : “Armin Glutch”}).hobbies
>db.passengers.find({hobbies:”sport”}).pretty()
>db.flightData.find({“status.description”:”on-time”}).pretty()
BITS Pilani, Hyderabad Campus
Questions??
BITS Pilani, Hyderabad Campus
Download