Indira Gandhi Delhi Technical University for Women Speakers: ● Ankur Raina ( ankur.raina@mongodb.com ) ● Pooja Gupta ( pooja.gupta@mongodb.com )
900+ employees About MongoDB, Inc. 4,300+ customers 19 offices worldwide
MongoDB Use Cases Single View Internet of Things Mobile Real-Time Analytics Catalog Personalization Content Management
AGENDA • Introduction to MongoDB – MongoDB Database – Document Model – BSON – Data Model – CRUD operations • Break (10 mins) • High Availability and Scalability – Replication – Sharding • Break (15 mins) • Hands-On MongoDB
MongoDB Database
MongoDB • Open-source, general purpose document database • Document: Field and Value pairs • Similar to JSON objects
Advantages • Documents (i.e objects) correspond to native datatypes in many programming languages • Reduce expensive joins by embedding • Dynamic schema - change on the fly
Document Unique Identification - Notice the fields
Document (contd.) Spot the difference! - Did you notice the flexibility?
JSON? BSON? • BSON is a binary-encoded serialization of JSON-like documents ( bsonspec.org ) • More data types such as BinData and Date • Lightweight, Traversable, Efficient
Data Model • Flexible Schema • Collections do not enforce Document structure • Consider the application usage patterns of data • Normalisation rules do not apply directly! • References and Embedding • 16 MB size limit of documents • Operation are atomic at document level
Let’s insert() a document in the collection
Let’s find() some documents from our collection
New Requirement • update() PAN numbers of citizens • Single PAN number of “some” citizens - Not everyone has a PAN card! • None of our documents has a “pan_card” field
• update() phone numbers of all citizens. Multiple phone numbers. • Note that we are using an array to store these
New Requirement • update() complete “permanent address” of citizens • A field named permanent_address containing sub-fields: – house_no – street – landmark – locality – district – state – pincode – map i.e. long-lat
Sub-documents & Geo-JSON
Relational TABLE 1 : CITIZEN_INFO id first_name last_name registered_on pan_card TABLE 2 : PHONE_NUMBERS person_id phone_number TABLE 3 : PERMANENT_ADDRESS person_id house_no street landmark locality pincode longitude latitude TABLE 4: PINCODE_LOOKUP pincode locality district state
Doesn’t it look like a natural fit for this data?
Let’s do some referencing • The government would like to keep track of criminal records associated with citizens
Executables
SQL -> MongoDB Structured Query Language (SQL) MongoDB Query Language (MQL) CREATE TABLE insert() / createCollection() ALTER TABLE - ADD COLUMN update() - $set ALTER TABLE - DROP COLUMN update() - $unset CREATE INDEX createIndex() DROP TABLE drop() INSERT INTO - VALUES insert() SELECT find() UPDATE - SET update() - $set DELETE remove() CreateReadUpdateDelete
Citizen Database Banking Application Sim Card Subscribers Gas Connection Subscription Biometric Details Example
Replica Set
Replica Set- Failure
Replica Set- Failover
Replica Set- Recovery
Replica Set- Recovered
Strong Consistency
Strong Consistency
Pros: ● Most of the software can easily take advantage of vertical scaling ● Easy to manage and install hardware within a single machine Pros: ● Increases performance in small steps as needed ● Can scale out the system as much as you need Cons: ● Requires substantial financial investment ● Not possible to scale up vertically after a certain limit Cons: ● Need to set up the additional servers to handle the data distribution and parallel processing capabilities
Hands-On MongoDB Download MongoDB Community Server: https://www.mongodb.com/download-center#community
mongo shell • Download from MongoDB Atlas (MongoDB database-as-a-service) • Connect to mongo shell - an interactive JavaScript interface to MongoDB • https://docs.atlas.mongodb.com/getting-started/
Let’s first restore data from an existing dump Don’t worry, if you don’t get this. Just follow the steps ! • Go to https://github.com/Ankur10gen/SampleDataMongoDB • Download the zip file and extract it • cd SampleDataMongoDB-master/mongodump-citizendata_09_10/ mongorestore --db <DBNAME> --host <”ReplicaSetName/Hosts1,Host2,Host3”> -- authenticationDatabase admin --ssl --username admin --password <PASSWORD e.g.: mongorestore --db demo1 demo1/ --host "Cluster0-shard-0/cluster0-shard- 00-00-ydjii.mongodb.net:27017,cluster0-shard-00-01- ydjii.mongodb.net:27017,cluster0-shard-00-02-ydjii.mongodb.net:27017" -- authenticationDatabase admin --ssl --username admin --password <PASSWORD>
Great! You have made it! You are ready to use the mongo shell now! Let’s see which databases exist, connect to a database & see the collections inside it. Note: There can be many databases in one mongod deployment and each database can have several collections. • show dbs • use demo1 • show collections
Ex1: Find one citizen with last_name ‘SHARMA’ > db.citizendata.findOne({last_name:"SHARMA"}) SELECT * FROM citizendata WHERE last_name = “SHARMA” LIMIT 1; Ex2: Find citizens with first_name ‘AJAY’ > db.citizendata.find({"first_name":"AJAY"}) SELECT * FROM citizendata WHERE first_name = “AJAY” Ex3: Limit the previous result set to 5 documents > db.citizendata.find({"first_name":"AJAY"}).limit(5) SELECT * FROM citizendata WHERE first_name = “AJAY” LIMIT 5
Ex4: When was person with "_id" : "678943212601" registered? > db.citizendata.find( { "_id": "678943212601" } , { "registered_on":1 } ) SELECT registered_on FROM citizendata WHERE _id = "678943212601"; Ex5: Find the count of people with state ‘HARYANA’. Note that state is a field inside permanent_address. > db.citizendata.find( { "permanent_address.state": "HARYANA" } ).count() YOU MAY NEED TO DO A JOIN AND WE DON’T WANT TO GO THERE. ======================================= enjoying?
Ex6: CREATE AN INDEX ON phone_numbers > db.citizendata.createIndex( { phone_numbers: 1 } ) CREATE INDEX phone_numbers_1 ON citizendata (phone_numbers) Ex7: Find details of a person with phone_number 8855915314. Note that phone_numbers is an array type field. > db.citizendata.find( { "phone_numbers": "8855915314" } ).pretty() Ex8: Find _id of citizens with first_name REVA or ABEER > db.citizendata.find( { "first_name": { "$in" : [ "REVA", "ABEER" ] } }, { _id: 1 } )
Ex9: Find the count of people with first_name SANDEEP in each state. We are using the MongoDB Aggregation Pipeline. > db.citizendata.aggregate( [ { $match : { "first_name":'SANDEEP' } }, { $group : { _id : "$permanent_address.state", count: {$sum: 1} } } ] ) In SQL, you’ll use a GROUP BY clause for it. And may be some joins to bring in this state info from another table.
Ex10: Let’s sort our citizens in descending order with last_name ‘VERMA’ on the basis of pan_card information using aggregation pipeline and limit our result set to 10. Project only the phone numbers with NO _id field. > db.citizendata.aggregate( [ { $match : { "last_name":'VERMA' } }, { $sort : { "pan_card" : -1 } }, { $project : { "_id": 0, "pan_card":1,"phone_numbers":1 } }, { $limit : 10 } ] )
I hope you enjoyed this session! Share your experience on the social networks! @MongoDB

Introduction to MongoDB at IGDTUW

  • 1.
    Indira Gandhi DelhiTechnical University for Women Speakers: ● Ankur Raina ( ankur.raina@mongodb.com ) ● Pooja Gupta ( pooja.gupta@mongodb.com )
  • 2.
  • 3.
    MongoDB Use Cases SingleView Internet of Things Mobile Real-Time Analytics Catalog Personalization Content Management
  • 4.
    AGENDA • Introduction toMongoDB – MongoDB Database – Document Model – BSON – Data Model – CRUD operations • Break (10 mins) • High Availability and Scalability – Replication – Sharding • Break (15 mins) • Hands-On MongoDB
  • 5.
  • 6.
    MongoDB • Open-source, generalpurpose document database • Document: Field and Value pairs • Similar to JSON objects
  • 7.
    Advantages • Documents (i.eobjects) correspond to native datatypes in many programming languages • Reduce expensive joins by embedding • Dynamic schema - change on the fly
  • 8.
  • 9.
    Document (contd.) Spot thedifference! - Did you notice the flexibility?
  • 10.
    JSON? BSON? • BSONis a binary-encoded serialization of JSON-like documents ( bsonspec.org ) • More data types such as BinData and Date • Lightweight, Traversable, Efficient
  • 11.
    Data Model • FlexibleSchema • Collections do not enforce Document structure • Consider the application usage patterns of data • Normalisation rules do not apply directly! • References and Embedding • 16 MB size limit of documents • Operation are atomic at document level
  • 12.
    Let’s insert() adocument in the collection
  • 13.
    Let’s find() somedocuments from our collection
  • 14.
    New Requirement • update()PAN numbers of citizens • Single PAN number of “some” citizens - Not everyone has a PAN card! • None of our documents has a “pan_card” field
  • 15.
    • update() phonenumbers of all citizens. Multiple phone numbers. • Note that we are using an array to store these
  • 16.
    New Requirement • update()complete “permanent address” of citizens • A field named permanent_address containing sub-fields: – house_no – street – landmark – locality – district – state – pincode – map i.e. long-lat
  • 17.
  • 18.
    Relational TABLE 1 :CITIZEN_INFO id first_name last_name registered_on pan_card TABLE 2 : PHONE_NUMBERS person_id phone_number TABLE 3 : PERMANENT_ADDRESS person_id house_no street landmark locality pincode longitude latitude TABLE 4: PINCODE_LOOKUP pincode locality district state
  • 19.
    Doesn’t it looklike a natural fit for this data?
  • 20.
    Let’s do somereferencing • The government would like to keep track of criminal records associated with citizens
  • 21.
  • 22.
    SQL -> MongoDB StructuredQuery Language (SQL) MongoDB Query Language (MQL) CREATE TABLE insert() / createCollection() ALTER TABLE - ADD COLUMN update() - $set ALTER TABLE - DROP COLUMN update() - $unset CREATE INDEX createIndex() DROP TABLE drop() INSERT INTO - VALUES insert() SELECT find() UPDATE - SET update() - $set DELETE remove() CreateReadUpdateDelete
  • 25.
    Citizen Database Banking Application SimCard Subscribers Gas Connection Subscription Biometric Details Example
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 34.
    Pros: ● Most ofthe software can easily take advantage of vertical scaling ● Easy to manage and install hardware within a single machine Pros: ● Increases performance in small steps as needed ● Can scale out the system as much as you need Cons: ● Requires substantial financial investment ● Not possible to scale up vertically after a certain limit Cons: ● Need to set up the additional servers to handle the data distribution and parallel processing capabilities
  • 38.
    Hands-On MongoDB Download MongoDB CommunityServer: https://www.mongodb.com/download-center#community
  • 39.
    mongo shell • Downloadfrom MongoDB Atlas (MongoDB database-as-a-service) • Connect to mongo shell - an interactive JavaScript interface to MongoDB • https://docs.atlas.mongodb.com/getting-started/
  • 40.
    Let’s first restoredata from an existing dump Don’t worry, if you don’t get this. Just follow the steps ! • Go to https://github.com/Ankur10gen/SampleDataMongoDB • Download the zip file and extract it • cd SampleDataMongoDB-master/mongodump-citizendata_09_10/ mongorestore --db <DBNAME> --host <”ReplicaSetName/Hosts1,Host2,Host3”> -- authenticationDatabase admin --ssl --username admin --password <PASSWORD e.g.: mongorestore --db demo1 demo1/ --host "Cluster0-shard-0/cluster0-shard- 00-00-ydjii.mongodb.net:27017,cluster0-shard-00-01- ydjii.mongodb.net:27017,cluster0-shard-00-02-ydjii.mongodb.net:27017" -- authenticationDatabase admin --ssl --username admin --password <PASSWORD>
  • 41.
    Great! You havemade it! You are ready to use the mongo shell now! Let’s see which databases exist, connect to a database & see the collections inside it. Note: There can be many databases in one mongod deployment and each database can have several collections. • show dbs • use demo1 • show collections
  • 42.
    Ex1: Find onecitizen with last_name ‘SHARMA’ > db.citizendata.findOne({last_name:"SHARMA"}) SELECT * FROM citizendata WHERE last_name = “SHARMA” LIMIT 1; Ex2: Find citizens with first_name ‘AJAY’ > db.citizendata.find({"first_name":"AJAY"}) SELECT * FROM citizendata WHERE first_name = “AJAY” Ex3: Limit the previous result set to 5 documents > db.citizendata.find({"first_name":"AJAY"}).limit(5) SELECT * FROM citizendata WHERE first_name = “AJAY” LIMIT 5
  • 43.
    Ex4: When wasperson with "_id" : "678943212601" registered? > db.citizendata.find( { "_id": "678943212601" } , { "registered_on":1 } ) SELECT registered_on FROM citizendata WHERE _id = "678943212601"; Ex5: Find the count of people with state ‘HARYANA’. Note that state is a field inside permanent_address. > db.citizendata.find( { "permanent_address.state": "HARYANA" } ).count() YOU MAY NEED TO DO A JOIN AND WE DON’T WANT TO GO THERE. ======================================= enjoying?
  • 44.
    Ex6: CREATE ANINDEX ON phone_numbers > db.citizendata.createIndex( { phone_numbers: 1 } ) CREATE INDEX phone_numbers_1 ON citizendata (phone_numbers) Ex7: Find details of a person with phone_number 8855915314. Note that phone_numbers is an array type field. > db.citizendata.find( { "phone_numbers": "8855915314" } ).pretty() Ex8: Find _id of citizens with first_name REVA or ABEER > db.citizendata.find( { "first_name": { "$in" : [ "REVA", "ABEER" ] } }, { _id: 1 } )
  • 45.
    Ex9: Find thecount of people with first_name SANDEEP in each state. We are using the MongoDB Aggregation Pipeline. > db.citizendata.aggregate( [ { $match : { "first_name":'SANDEEP' } }, { $group : { _id : "$permanent_address.state", count: {$sum: 1} } } ] ) In SQL, you’ll use a GROUP BY clause for it. And may be some joins to bring in this state info from another table.
  • 46.
    Ex10: Let’s sortour citizens in descending order with last_name ‘VERMA’ on the basis of pan_card information using aggregation pipeline and limit our result set to 10. Project only the phone numbers with NO _id field. > db.citizendata.aggregate( [ { $match : { "last_name":'VERMA' } }, { $sort : { "pan_card" : -1 } }, { $project : { "_id": 0, "pan_card":1,"phone_numbers":1 } }, { $limit : 10 } ] )
  • 47.
    I hope youenjoyed this session! Share your experience on the social networks! @MongoDB