Understanding basics of MongoDB and MySQL PSOSM summer school @ IIITH Divyansh Agarwal - Research Associate 3rd July, 2017 Precog Labs, IIIT-Delhi
What is a Database? Organized collection of data. Collection of schemas, tables, queries, reports, views, and other objects. Database Management Systems (DBMS) A software application which allows: Definition, Creation, Querying, Update, and Administration of databases
Why DBMS and not flat files? A Database Management System allows: Handling queries. Handling indexing. Handling access from network. Providing access control (user and/or privileges). Convenient data manipulation.
Types of DBMS to talk about Relational DBMS (SQL) NoSQL DBMS
Relational Database Management Systems (RDBMS) DBMS with relational structure such as tables, columns, rows etc. Supports ACID properties: Atomicity: Transaction requires to follow - All or Nothing Consistency: Transaction to bring DB from one valid state to another. Isolation: Transactions must act sequentially. Durability: A committed transaction must never be lost.
What is MySQL? Open-source, fast and easy-to-use RDBMS ACID compliant Supports large databases (upto 50 million rows or more in a table) Uses Structured Query Language (SQL)
Installing MySQL on your machines If not installed, open your terminals, please! sudo apt-get update sudo apt-get install mysql-server sudo mysql_secure_installation sudo mysql_install_db Check version! mysql --version
MySQL Shell Login Type: mysql -u <username> -p
Creating a database Type: create database <database_name> Show databases
Creating Tables Type: create table <table_name> (column_name, column_type); Drop Table Type: drop table <table_name>
Inserting data into Table Type: insert into <table_name> (field1, field2,...fieldn) values (value1, value2,...valuen); Find all tables in a db Type: show tables;
Querying data from table Type: SELECT field1, field2,...fieldn from <table_name1<, <table_name2>... [WHERE Clause]; Selecting all records from a table : SELECT * from <table_name>
Update data in table Type: update <table_name> set field1 = new_value1, field2 = new_value2 [where clause]
Delete a record from table Type: DELETE FROM table_name [WHERE Clause]
Exporting data from MySQL mysqldump -u root -p database_name table_name > dump.txt
What is MongoDB? MongoDB is an open source, cross-platform, document oriented, database program. Written in C++. It is a NoSQL database program, which uses JSON-like documents to store data.
NoSQL Database NoSQL stands for Not Only SQL. A NoSQL database provides a mechanism for storage and retrieval of data which is modeled in means other than the tabular relations used in relational databases. The fundamental difference between MongoDB and an RDBMS is the underlying data model - A relational database structures data into tables and rows, while MongoDB structures data into collections of JSON documents.
Why use NoSQL databases? Some operations are faster in NoSQL databases owing to the different kind of data structures used for modelling the data. For example, NoSQL uses data structures like document based, key-value pairs, graph databases. Data structures used are viewed as more flexible than relational database tables. NoSQL databases are simpler in design, and a good fit for non-complex queries.
Salient features of MongoDB Document oriented Cross platform NoSQL Free and open source Horizontally scalable Dynamic schema, useful for unstructured data. Client libraries available for all major programming languages - PHP, Python, C++, Ruby etc.
Let s dive deeper into MongoDB!
Installing mongodb on your machine sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list sudo apt-get update sudo apt-get install -y mongodb-org (MongoDB should automatically install after this.) sudo service mongod status - to check if mongo instance is running or not sudo pip install pymongo (Python wrapper for mongo)
Using MongoDB Activate the mongo shell by simply typing mongo on the terminal window. use DATABASE_NAME is used to create database. To check your currently selected database, use the command db. If you want to check your databases list, use the command show dbs. MongoDB structure consists of Databases Collections Documents. To delete a particular database, switch to that database and run - db.dropdatabase()
Inserting data into MongoDB Create a collection in your database - db.createcollection("participants") and insert a document - db.participants.insert( {"name":"your name"} ) Automatically create collection and add document - db.presenters.insert( {"name":"divyansh"} ) show collections - To view the collections in your mongodb Delete a particular collection - db.collection_name.drop()
Or instead, take a json document - post = { name : Divyansh, age : 22, college : IIIT-D, email: divyansha@iiitd.ac.in, interests: [ Social computing, DBMS, ML ], likes: 100 } db.participants.insert( post )
Querying data from MongoDB db.collectionname.find() - Lists all the documents in the collection db.collectionname.findone() - Lists one document from the collection db.collectionname.find()[2] - Lists one specific document from the collection db.collectionname.findone().pretty() - Prettify the document db.collectionname.find().limit(number) - Display specified number of documents
Advanced querying Conditional query syntax - db.collectionname.find( {<key>:<value>} ) Example : db.collectionname.find( {name: Divyansh } ) Pretty print - db.collectionname.find( {name: Divyansh } ).pretty() db.mycol.find({name:{$ne: Divyansh }}) - Fetch documents with names except Divyansh.
Updating documents in MongoDB Basic syntax - db.collectionname.update(selection_criteria, Updated_Data) db.collectionname.update({'title':'new_title'},{$set:{'title':'new_name'}}) To update multiple documents in one command - db.collectionname.update({'title':'new_title'}, {$set:{'title':'new_name'} },{multi:true})
Removing a document in MongoDB db.collectionname.remove( {<key>:<value>} ) - Removes all the documents which matches the given condition. db.collectionname.remove( {<key>:<value>}, 1) - Removes one document which matches the given condition. db.presenters.remove({ name : Divyansh },1) - Remove the document where the name is Divyansh
Exporting data from mongodb Exporting to json : mongoexport --db <db_name> --collection <collection_name> --out filename.json mongoexport --db new_db --collection mycollection --out mycollection.json Taking a dump of mongo: mongodump --db <db_name> Exporting to CSV: Convert bson dump to csv using - bsondump collection.bson > file.csv Using mongoexport: mongoexport --db <db_name> --collection <collection_name> --type=csv --fields=field1,field2,field3 --out filename.csv
MySQL v/s MongoDB MongoDB uses dynamic schemas, meaning that you can create records without first defining the structure, such as the fields or the types of their values. Whereas, MySQL first defines the schema, and then enters records into the table, based on the schema. MySQL is vertically scalable, whereas MongoDB is horizontally scalable. MongoDB has a more flexible data model compared to MySQL. This means that the database schema can evolve with the requirements of the application. Schema changes that might take hours to change in a MySQL data base, take only a few hours with MongoDB.
Development is simplified with MongoDB, but there are use cases for which a relational database like MySQL would be better suited. SQL also takes care of data redundancy issues, whereas MongoDB doesn t. SQL implements data integrity, whereas MongoDB permits any kind of data to be saved in a document field. There are many examples of hybrid deployments of MongoDB and MySQL. Many e-commerce applications use a combination of MongoDB and MySQL.
Thank you! divyansha@iiitd.ac.in precog.iiitd.edu.in