MongoDB An introduction. What is MongoDB? The name Mongo is derived from Humongous To say that MongoDB can handle a humongous amount of data Document.

Slides:



Advertisements
Similar presentations
1 Constraints, Triggers and Active Databases Chapter 9.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Systems: Design, Implementation, and Management Tenth Edition
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Introduction to Structured Query Language (SQL)
Introduction to PHP. PHP Origins Rasmus LerdorfRasmus Lerdorf (born Greenland, ed Canada) PHP originally abbreviation for ‘Personal Home Pages’, now ‘PHP.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
A Social blog using MongoDB ITEC-810 Final Presentation Lucero Soria Supervisor: Dr. Jian Yang.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation MongoDB Read Lecturer.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation MongoDB Write Lecturer.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation MongoDB Data Modeling.
MONGODB NOSQL SERIES Karol Rástočný 1. Prominent Users 2  AppScale, bit.ly, Business Insider, CERN LHC, craigslist, diaspora, Disney Interactive Media.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
ASP.NET Programming with C# and SQL Server First Edition
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Internet Forms and Database Bob Kisel Amgraf, Inc.
NoSQL continued CMSC 461 Michael Wilson. MongoDB  MongoDB is another NoSQL solution  Provides a bit more structure than a solution like Accumulo  Data.
WTT Workshop de Tendências Tecnológicas 2014
Goodbye rows and tables, hello documents and collections.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation MongoDB Aggregation.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
WEEK 1, DAY 2 STEVE CHENOWETH CSSE DEPT CSSE 533 –INTRO TO MONGODB.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Advanced SQL: Triggers & Assertions
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
MongoDB First Light. Mongo DB Basics Mongo is a document based NoSQL. –A document is just a JSON object. –A collection is just a (large) set of documents.
Mongodb.org A. Im, G. Cai, H. Tunc, J. Stevens, Y. Barve, S. Hei Vanderbilt University.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Lesson 4: Querying a Database. 2 Learning Objectives After studying this lesson, you will be able to:  Create, save, and run select queries  Set query.
Introduction to MongoDB. Database compared.
Aggregator Stage : Definition : Aggregator classifies data rows from a single input link into groups and calculates totals or other aggregate functions.
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Exam Friday April 11. MongoDB Specifics Find() to Query db.collection.find(, ) db.collection.find{{select conditions}, {project columns}) Selection conditions:
COMP 430 Intro. to Database Systems MongoDB. What is MongoDB? “Humongous” DB NoSQL, no schemas DB Lots of similarities with SQL RDBMs, but with more flexibility.
Mongo Database (Intermediate)
NO SQL for SQL DBA Dilip Nayak & Dan Hess.
and Big Data Storage Systems
Lecturer : Dr. Pavle Mogin
MongoDB Er. Shiva K. Shrestha ME Computer, NCIT
MongoDB CRUD Operations
javascript for your data
Aggregation Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together,
NOSQL databases and Big Data Storage Systems
MongoDB Read/Write.
MongoDB Read/Write.
Contents Preface I Introduction Lesson Objectives I-2
CS5220 Advanced Topics in Web Programming Introduction to MongoDB
Chapter 8 Advanced SQL.
INTRODUCTION TO MONgodb
Presentation transcript:

MongoDB An introduction

What is MongoDB? The name Mongo is derived from Humongous To say that MongoDB can handle a humongous amount of data Document database In mongo you store documents Each document is identified by a key (key, document) Documents are written using BSON BSON is an extension of JSON (JavaScript Object Notation) MongoDB, an introduction2

Example BSON document { …. } Curly braces encloses a document Name : value Field Names are optionally quoted _id The object id Automatically generated [ … ] Array of documents / objects. Heterogeneous array May contain documents of different types MongoDB, an introduction3

Some BSON data types Double String Object Array Binary data Object id Boolean True or False Null Integer, 32 bit Integer, 64 bit Date 64 bit Integer Number of milliseconds since Jan 1, 1970 Unix birthday! Date() is now ISODate(” T01:45:20+01:00”) +01 is distrance from UTC/GMT Regular expression JavaScript Reference nce/bson-types/ MongoDB, an introduction4

The ObjectId data type 12 bytes, often writing using hexadecimal [0-9a-f] 4 bytes timestamp (time for object creation) 3 bytes machine identifier 2 bytes process identifier 3 byte counter Reference MongoDB, an introduction5

MongoDB terms Database Holds of a set of collections Each database has a name Collection Holds a set of documents. Dynamic schema / heterogeneous collection A collection can hold documents with different structures Each collection has a name Document A set of (key, value) pairs Each document has an ObjectId MongoDB, an introduction6

Capped collections Capped collection Fixed size Full: Make room for new document, by overwriting the oldest document Like circular buffer Insertion order is preserved Usage example: Log Creation Db.createCollection(”log”, {capped: true, size:10000, max:1000} ); Source MongoDB, an introduction7

Atomic write operations Relational DBMS has transactions ACID properties MongoDB has atomic write operations Only one process can update a single document or collection at the same time Updating a document + a referenced document is two writes Not atomic MongoDB, an introduction8

Terms, MongoDB vs. Relational DBMS MongoDBRelational DBMS CollectionTable BSON documentRow BSON fieldColumn Index Embedded documentJoin ShardPartition Shard keyPartition key MongoDB, an introduction9

Query language / API Find documents in a collection db.books.find(…) Save documents to a collection Db.collection.save(…) Db.collection.insert(…) Update documents in a collection Db.collection.update(…) Remove documents from a collection Db.collection.remove(…) MongoDB, an introduction10

Save(…) vs. insert(…) Db.collection.save(document) Saves a document to the collection. Example, without _id db.books.save( { title: "Beginning Android 4", pages : 531 }); The DBMS create a value for the _id field Example, with _id db.books.save( { _id : 123, title: "Oracle PL/SQL Programming" }); If the _id:123 exists the new document will replace the old document Source b.collection.save/ Db.collection.insert(document) Inserts a document into the collection Example, without _id db.books.insert( { title: "Oracle PL/SQL Programming" }); The DBMS create a value for the _id field Example, with _id db.books.insert( { _id: 123, title: "Mastering Oracle SQL" }); If the _id:123 exists the new document will be rejected. … duplicate key error … Source b.collection.insert/ MongoDB, an introduction11

Update(…) Update one or more documents in a collection … or replace document(s) General syntax Db.collection.update(query, update, options) Query: selection criteria for the update Which documents are updated Update: The updates to apply Source erence/method/db.collection.update / Example db.books.update( { title: "Beginning Android 4“ }, { title: "Beginning Android 4 Apps", publisher: "Wrox" }, { upsert: false // no match, no update } ); MongoDB, an introduction12

Remove(…) General syntax Db.collections.remove(query, justOne) Query: Query: selection criteria for the update Which documents will be removed justOne: Boolean One or more documents to be removed Optional. Default false Example db.books.remove( { title: "Mastering Oracle SQL" } ) Source MongoDB, an introduction13

find(…) General syntax: db.collection.find(criteria, projection) Criteria: optional Selection criteria: Which documents do you want to get Projection: optional Projections operators: Which fields of the documents do you want to get SQL analogy: SELECT projection FROM table WHERE selection Find all documents Db.collection.find() Find with criteria db.books.find({ publicationYear: 2014 }); Books where the publicationYear fields has the value 2014 db.books.find({ pages: { $gt: 25 } }); Books where the pages field has a value greater than 25 Source MongoDB, an introduction14

Find(…) with and + or Find(…) with and criteria db.books.find({publicationYear: 2015, title: /^Mongo/}) publicationYear = 2015 AND title starts with (^) Mongo /^Mongo/ is a regular expression Find(…) with or criteria db.books.find({$or: [ {publicationYear: 2015}, {title: /^Mongo/}]}) publicationYear = 2015 OR title starts with A $or: [ … array of conditions … ] db.books.find({$or: [ {publicationYear: 2015}, {publicationYear: {$exists: false}}]}) Find(…) with and criteria (a little more …) $and: [ … array of conditions … ] Find({cond, cond,.. }) works the same However with $and: you can use the same attribute more than once db.books.find({$and: [{publicationYear: {$ne:2014}}, {publicationYear: {$exists: true}}]}) publicationYear exists, but is not equal ($ne) to 2014 Source MongoDB, an introduction15

Find(…), getting into sub-documents Use the dot (.) operator to get into sub-documents db.books.find({"author.firstname" : "John"}); Documents with an author with the first name John Note: Author is an array Note 2: The name of the field (author.firstname) must be quoted db.books.find( {$or: [ {"author.firstname" : "John"}, {"author.firstname" : "Brad"} ] } ); MongoDB, an introduction16

find(…), projections Include fields db.books.find({}, {title: 1, publisher: 1}); {} no selection = all documents Title, publisher (and _id) included in the output Exclude fields db.books.find({}, {title: 0}); All fields, but title, in the output MongoDB, an introduction17

Find() with sort(), limit(), and skip() The output can be sorted db.books.find().sort({title: 1, publicationYear: 1}); Sort by 1. title, 2. publicationYear The output can be limited db.books.find().sort({title: 1}).limit(4); Only the first 4 documents are included in the output You can skip parts of the output db.books.find().sort({title: 1}).skip(2).limit(3); Two documents are skipped. Next 3 documents are included in the output MongoDB, an introduction18

Counting documents You can count the number of documents in a collection db.books.count() Counts all documents in the books collection db.books.count({title: /Mongo/}); Counts all document where the title contains “Mongo” Source MongoDB, an introduction19

Distinct values Getting all the values of arrays in a collection db.books.distinct("author.firstname") All first names, from all books. Distinct, means no duplicates Can be combined with sorting db.books.distinct("author.firstname").sort(); MongoDB, an introduction20

References Relational DBMS Foreign keys refer to primary keys (or unique attributes) Relational integrity: The referenced primary key must exist Join: Very often done on foreign key – primary key MongoDB: No join De-normalized documents: Related data is stored inside the document Sometimes the related data is stored in another document The documents refers to the _id of the other document Source MongoDB, an introduction21

Insert, using reference original_id = ObjectId() // make an id db.books.insert( { _id : original_id, title: "My very own book, 1st edition" }) db.books.insert( { title: "My very own book, 2nd edition", prev_edition_id : original_id // refer to the other id }) MongoDB, an introduction22

Functions MongoDB can store functions Db.system.js.save({ _id: functionName, value: theFunction }); System.js is a special collection Programming language: JavaScript db.loadServerScripts(); Loads the scripts / functions Necessary before calling a function Source server/ server/ MongoDB, an introduction23

Cursors and iteration The find(…) method returns a cursor. This cursor can be iterated using the forEach(…) method db.books.find().forEach(function(book) {print("Book: " + book.title);}); Function defined inside forEach Another example function(element) { print(element.title); } db.books.find().forEach(printTitle); Function defined and stored in the database Source MongoDB, an introduction24

Map-Reduce Idea invented by Google, 2004 General syntax mapReduce(map function, reduce functions, arguments) Map function Maps all documents into a hash table (key, values) Reduce function Reduces the values to a single value. Like a sum or an average of the values. Arguments Defines how to get the result document Similar to SQL Select … group by … Sources MongoDB, an introduction25

Map-Reduce, a simple example Example var mapFunction = function() { emit(this.publisher, 1); }; var reduceFunction = function(key, value) { return Array.sum(value); } var res = db.books.mapReduce(mapFunction, reduceFunction, { out: "map_reduce_example" }); db.map_reduce_example.find(); MongoDB, an introduction26

Map-Reduce, another simple example Example var mapFunction = function() { emit(this.publisher, this.pages); }; var reduceFunction = function(key, value) { return Array.avg(value); } var res = db.books.mapReduce(mapFunction, reduceFunction, { out: "map_reduce_example" }); db.map_reduce_example.find(); MongoDB, an introduction27

MongoDB, usage MongoDB can be used in a lot of different contexts Source common-tasks-for-mongodb.html MongoDB, an introduction28