Mongo Database (Intermediate)

Slides:



Advertisements
Similar presentations
CC SQL Utilities.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
MONGODB NOSQL SERIES Karol Rástočný 1. Prominent Users 2  AppScale, bit.ly, Business Insider, CERN LHC, craigslist, diaspora, Disney Interactive Media.
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.
Goodbye rows and tables, hello documents and collections.
INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation MongoDB Aggregation.
WEEK 1, DAY 2 STEVE CHENOWETH CSSE DEPT CSSE 533 –INTRO TO MONGODB.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Introduction to MongoDB. Database compared.
1 Working with MS SQL Server Beginning ASP.NET in C# and VB Chapter 12.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
N5 Databases Notes Information Systems Design & Development: Structures and links.
Introduction to Mongo DB(NO SQL data Base)
PHP using MySQL Database for Web Development (part II)
Creating Databases for Web applications
Web Database Programming Using PHP
CPSC-310 Database Systems
Computer Science Department
Data Indexing Herbert A. Evans.
Course Developer/Writer: A. J. Ikuomola
CS320 Web and Internet Programming SQL and MySQL
Managing Tables, Data Integrity, Constraints by Adrienne Watt
INLS 623– Database Systems II– File Structures, Indexing, and Hashing
Lecturer : Dr. Pavle Mogin
Data Definition and Data Types
CC410: System Programming
MongoDB Er. Shiva K. Shrestha ME Computer, NCIT
Containers and Lists CIS 40 – Introduction to Programming in Python
Web Database Programming Using PHP
Data Definition and Data Types
Dineesha Suraweera.
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,
Designing Tables for a Database System
Intro To Design 1 Elementary School Library: User Sub-System Class Diagrams Software Engineering CSCI-3321 Dr. Tom Hicks Computer Science Department.
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Lecture 15: Bitmap Indexes
CIS16 Application Development and Programming using Visual Basic.net
PHP.
Web DB Programming: PHP
CIS16 Application Programming with Visual Basic
MongoDB Aggregations.
MongoDB Read/Write.
CS122 Using Relational Databases and SQL
CSE 482 Lecture 5: NoSQL.
Contents Preface I Introduction Lesson Objectives I-2
CS5220 Advanced Topics in Web Programming Introduction to MongoDB
Spreadsheets, Modelling & Databases
Building applications with MongoDB – An introduction
Tutorial 6 PHP & MySQL Li Xu
Relational Database Design
CS3220 Web and Internet Programming SQL and MySQL
CS1222 Using Relational Databases and SQL
Introduction to Data Structure
JavaScript CS 4640 Programming Languages for Web Applications
INTRODUCTION TO MONgodb
MongoDB Aggregations.
IST 318 Database Administration
PHP an introduction.
CS3220 Web and Internet Programming SQL and MySQL
Shelly Cashman: Microsoft Access 2016
CS122 Using Relational Databases and SQL
Presentation transcript:

Mongo Database (Intermediate) Database Systems CSCI-3343 Dr. Tom Hicks Computer Science Department

Execute Each & Every Query As We Go!

Create Backup Directory

Create A Folder Called "C:\Mongo Backups"

Create A Folder Called "C:\Mongo Backups\Scripts"

Add Our Scripts

Make "C:\Mongo Backups" Your Current Directory Now Start Mongo!

Lots Of Review

Execute Each & Every Query As We Go!

List Databases

How Do We List The Databases show dbs

Set The Default Database

How Do We Make military The Default Database use military

Mongo Terminology

The Equivalent Of Tables In Mongo Is _?_ Collections How Do You List The Collections In The Default Database

The equivalent of record/tuple/row, in Mongo, are _?_ Documents

_?_ is a 12 bytes hexadecimal number which assures the uniqueness of every document. _id

Mongo Searching

You probably have some consultants left over from lecture last time You probably have some consultants left over from lecture last time. List all of the information about all of the collections. db.consultant.find()  Do any of you have this? db.consultant.find({})  Do any of you have this?

How can you show all of the information about consultant Po? db.consultant.find({name: "Po"}) db.consultant.find({rank: "Colonel"})  May Have More Than Po!

Delete A Document

How can you delete the consultant Po? db.consultant.remove({name: "Po"})

Adding A Document

Add Consultant  Mark  General db.consultant.insert({name: "Mark", rank: "General"})

Add Consultant  Paul db.consultant.insert({name: "Paul"})

Documents In Sorted Order

List All Of The Consultants In Order By Name db.consultant.find().sort({name:1})

Do All Documents Have To Have The Same Fields?

Do You Think This Query Will Work? No  Name No  Rank ? You Don't Need To Do This One!

It Appears To Work?

List All Of The Consultants In Order By Name db.consultant.find().sort({name:1})

Display With A Nice Layout

List All Of The Consultants In Order By Name Better Way To Render The Jason Scripts? db.consultant.find().sort({name:1}).pretty()

Advantages Of Mongo Database

Mongo Advantages Over RDBMS -1 Schema less: MongoDB is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another. Structure of a single object is clear. No complex joins. Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.

Mongo Advantages Over RDBMS - 2 Ease of scale-out: MongoDB is easy to scale. Conversion/mapping of application objects to database objects not needed. Uses internal memory for storing the (windowed) working set, enabling faster access of data.

File NewConsultants.txt

Create NewConsultants.txt

Why Use Mongo?

Why Use Mongo? Document Oriented Storage: Data is stored in the form of JSON style documents. Index on any attribute Replication and high availability Auto-sharding Rich queries Fast in-place updates "Sharding" is a method for distributing data across multiple machines

Remove All Items From A Collection

How can you delete all of the consultants, but not the collection" db.consultant.remove({})

Batch Process A Set Of Queries

Perform These Queries

Display Only Some Of The Document Fields

List The Name & Email Of All Of The Consultants In Order By Name db.consultant.find({}, {name:1, email:1, _id:0}).sort({name:1})

List The Collections

List All Of The Collections In The Military Database show collections

Create A Collection

Create A Collection db.createCollection("Gamers")

Look Through Manals  Often Optional Arguments

Backup A Mongo Database

Backup A Mongo Database mongodump --db military  several ways to do

Check The Backup?

Delete A Database

Delete the military database db.dropDatabase()

Restore A Database

Restore Database military mongorestore --drop -d military dump\military

Delete A Collection

Delete The consultant Collection db.consultant.drop()

Reload From File NewConsultants.txt

Create NewConsultants.txt

Display Only Some Of The Document Fields

List The name & mid Of All Of The Consultants db.consultant.find({}, {name:1, mid:1, _id:0})

Count

List The Number Of Consultants db.consultant.find({}).count()

Search Query Greater Than

List The name & mid Of Those Consultants Whose mid > 1013 In Order By name db.consultant.find({mid: {$gt: 1013}}, {name:1, mid:1, _id:0}).sort({name:1})

List The The Number Of Consultants Whose mid > 1013 db.consultant.find({mid: {$gt: 1013}}).count()

Search Query Greater Than Or Equal To

List The name & mid Of Those Consultants Whose mid >= 1013 In Order By name db.consultant.find({mid: {$gte: 1013}}, {name:1, mid:1, _id:0}).sort({name:1})

List The The Number Of Consultants Whose mid >= 1013 db.consultant.find({mid: {$gte: 1013}}).count()

Search Query Less Than

List The name & mid Of Those Consultants Whose mid < 1006 In Order By mid db.consultant.find({mid: {$lt: 1006}}, {name:1, mid:1, _id:0}).sort({mid: 1})

List The The Number Of Consultants Whose mid < 1006 db.consultant.find({mid: {$lt: 1006}}).count()

Search Query Less Than Or Equal To

List The name & mid Of Those Consultants Whose mid <= 1006 In Order By mid db.consultant.find({mid: {$lte: 1006}}, {name:1, mid:1, _id:0}).sort({mid: 1})

List The The Number Of Consultants Whose mid <= 1006 db.consultant.find({mid: {$lte: 1006}}).count()

Search Query Not Equal To

List The name & mid Of Those Consultants Whose mid <> 1013 In Order By name db.consultant.find({mid: {$ne: 1013}}, {name:1, mid:1, _id:0}).sort({name:1})

List The The Number Of Consultants Whose mid <> 1006 db.consultant.find({mid: {$ne: 1006}}).count()

Search Query Equal To

List The name & mid Of Those Consultants Whose mid = 1013 In Order By name db.consultant.find({mid: 1013}, {name:1, mid:1, _id:0}).sort({name:1})

Search Query AND

List The name, rank, & mid Of Those Consultants Whose mid > 1004 AND Are Capitan In Order By name #1 Solve In Parts When Complex:  mid > 1004 db.consultant.find ({mid: {$gte: 1004}}, {_id:0, mid:1})

List The name, rank, & mid Of Those Consultants Whose mid > 1004 AND Are Capitan In Order By name #2 Solve In Parts When Complex:  mid > 1004 & captain db.consultant.find ({$and: [{mid: {$gte: 1004}}, {rank: "Captain"}]}, {_id:0, rank:1, mid:1})

List The name, rank, & mid Of Those Consultants Whose mid > 1004 AND Are Capitan In Order By name #3 Solve In Parts When Complex:  complete db.consultant.find ({$and: [{mid: {$gte: 1004}}, {rank: "Captain"}]}, {_id:0, name:1, rank:1, mid:1}).sort({name:1})

Search Query OR

List The name, rank, & mid Of Those Consultants Whose Are Capitan or Colonel In Order By name db.consultant.find ({$or: [{rank: "Captain"}, {rank: "Colonel"}]}, {_id:0, name:1, rank:1, mid:1}).sort({name:1})

Search Query Substring

List The Consultant names That Have An 'a' In Their Name db.consultant.find({name: {$regex: "a"}}, {_id:0, name:1})

List The names Of All Consultant names That Have An 'an' In Their Name db.consultant.find({name: {$regex: 'an'}}, {_id:0, name:1})

List The names Of All Consultant names That Have An 'ie' In Their Name db.consultant.find({name: {$regex: "ie"}}, {_id:0, name:1})

List The names Of All Consultant names That Have An 'C/c' In Their Name db.consultant.find({$or: [{name: {$regex: "C"}}, {name: {$regex: "c"}}]}, {_id:0, name:1})

Search Query Case Insensitive Search

List The Consultant names That Match miCHaeL  Case Insensitive db.consultant.find({name: /miCHaeL/i}, {_id:0, name: 1})

List The Consultant names That Contain C/c  Case Insensitive db.consultant.find({name: /c/i}, {_id:0, name: 1})

List The Consultant names That Contain C/c  Case Insensitive db.consultant.find({name: /.c/i}, {_id:0, name: 1})

Mongo Datatypes

Mongo Has Many Datatypes #1 String: This is the most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid. Integer: This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server. Boolean: This type is used to store a boolean (true/ false) value. Double: This type is used to store floating point values. Min/Max Keys: This type is used to compare a value against the lowest and highest BSON elements.

Mongo Has Many Datatypes #2 Arrays: This type is used to store arrays or list or multiple values into one key. Timestamp: ctimestamp. This can be handy for recording when a document has been modified or added. Object: This datatype is used for embedded documents. Null: This type is used to store a Null value. Symbol: This datatype is used identically to a string; however, it's generally reserved for languages that use a specific symbol type.

Mongo Has Many Datatypes #3 Date: This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it. Object ID: This datatype is used to store the document’s ID. Binary Data: This datatype is used to store binary data. Code: This datatype is used to store JavaScript code into the document. Regular Expression: This datatype is used to store regular expression.

Update Query A Single Document

Promote consultant Evan To General db.consultant.update({name: "Evan"}, {$set: {rank: "General"}})

Change Brent's mid to 1015 db.consultant.update({name: "Brent"}, {$set: {mid: 1015}})

Update Increment Query

Increment consultant Evan's mid by 1 db.consultant.update({name: "Evan"}, {$inc: {mid: 1}})

Increment consultant Evan's mid by 3 db.consultant.update({name: "Evan"}, {$inc: {mid: 3}})

Decrement consultant Evan's mid by  Increment by -4 db.consultant.update({name: "Evan"}, {$inc: {mid: 3}})

Update Multiply Query

Double consultant Samuel mid db.consultant.update({name: "Samuel"}, {$mul: {mid: 2}})

Half consultant Samuel mid db.consultant.update({name: "Samuel"}, {$mul: {mid: .5}})

Update Query Multiple Documents

Double mid For All consultants db.consultant.update({name: "Samuel"}, {$mul: {mid: 2}}, {multi: true})

Replace Document Query

Replace consultant Tom With  Tom - Private db.consultant.save({_id : ObjectId("583a1ba051ae2637bd82b573"), name: "Tom", rank: "Private", wife: "Sherry"})  By ObjectID!  If No ObjectID match  Does Insert

Create File NewerConsultant.txt

Create File NewerConsultants.txt Replace Collection consultant

Set No To Display DBQuery.shellBatchSize

Display Name, Rank, Deleted For All  it!

Set The Number To Display (without IT) DBQuery.shellBatchSize = 100

Ordered Data Query

List Name & Rank & Deleted for All Consultants  Order By Rank db.consultant.find ({}, {_id:0, name:1, rank:1}).sort({rank:1})

List Name & Rank & Deleted for All Consultants  Order By Rank By Name db.consultant.find ({}, {_id:0, name:1, rank:1}).sort({rank:1, name:1})

Query View

View #1 : all consultants in order by name  skip _id field db.consultant.find ({}, {_id:0, name:1, mid:1, rank:1, server:1, email:1, deleted:1}).sort({name:1})

View #2 : how many consultants (total?) db.consultant.find ().count() View #3 : how many deleted consultants db.consultant.find ({deleted: 'T'}).count() View #4 : how many non-deleted consultants db.consultant.find ({deleted: 'F'}).count()

Now Generate The Equivalent View In Mongo RDMS VIEW SELECT * FROM consultants WHERE (Deleted = 'F') ORDER BY name Now Generate The Equivalent View In Mongo

View: All non-deleted consultants in order by name  Query? db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, email:1}).sort({rank:1, name:1})

Limit Query

Use Limit 4 In Our View db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, email:1}).sort({rank:1, name:1}).Limit(4)

Use Limit 1 In Our View db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, email:1}).sort({rank:1, name:1}).Limit(1) As I Mentioned In The Last Set Of Slides, "Limit" accepts only one argument and cannot be used, by itself, to identify the 37th document within our view!

Skipping Query

Run The Following Query db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, email:1}).sort({name:1, rank:1}).skip(5).limit(1) What Is The Significance Of This Query? MySQL  Limit 5,1

Drivers: C, C++, C#, .Net, PHP, Python, Scala, Ruby, Pearl, Java, etc.

Mongo Indexing

Index Same Types Of Things That We Do For RDMS

What Data Structure Do You Suppose Mongo Uses For Their Indexing? B+ Tree  Since No One Uses B Trees, Everyone Simply Refers To The Structure As A B-Tree

Indexing Is A Critical Part Of All Database Design Indexing Critical!  Will Not Matter On Our Simple Databases

Create A consultant Index On name db.consultant.ensureIndex ({name:1})

List The Indexes db.consultant.getIndexes()

Another Way To Create An Index db.consultant.createIndex({mid:1})

Start Robomongo  Check Out Your Indexes!

Rebuild Index? Can Take Hours!

Robomongo To Create An Index #1 Please Do This!

Robomongo To Create An Index #2 Please Do This!

Search Query Wild Card Search

Awkward Queries db.consultant.aggregate( [ { $group: { _id: null, total : { $sum: "$mid" } high: { $max: "$mid" } } } ] )