0% found this document useful (0 votes)
4 views

Databases

The document provides an overview of databases, highlighting the differences between SQL and NoSQL, and detailing CRUD operations. It specifically focuses on MongoDB, a NoSQL database, and its features, including the use of ObjectID, cursors, and the Mongoose ORM for data management. Additionally, it introduces Robomongo as a client tool for MongoDB and includes basic SQL queries and commands for database operations.

Uploaded by

Kartik Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Databases

The document provides an overview of databases, highlighting the differences between SQL and NoSQL, and detailing CRUD operations. It specifically focuses on MongoDB, a NoSQL database, and its features, including the use of ObjectID, cursors, and the Mongoose ORM for data management. Additionally, it introduces Robomongo as a client tool for MongoDB and includes basic SQL queries and commands for database operations.

Uploaded by

Kartik Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Database.

• SQL vs NoSQL
• CRUD Operations.
• MongoDB.
• Robomongo.
Table of Contents
• What is Database?
• Use of Databases.
• SQL vs NoSQL
• Basic SQL Queries.
• CRUD Operations.
• MongoDB.
• Robomongo.
Table of Contents
• Node MongoDB Native.
• ObjectID.
• Object Restructuring and De-structuring.
• Cursor in MongoDB.
• MongoDB - Client.
• Mongoose ORM.
What is Database?
•.A database is an organized collection of data.
• It generally stores and access electronically from a computer system.
• It is usually controlled by Database Management System (DBMS).
• In database, the data can be easily accessed, managed, modified,
controlled, updated and organized with ease.
• Most databases use Structured Query Language (SQL) for writing and
querying the collective data(s).
• Database can be modeled in rows and columns in a series of tables.
• It process the data and query it efficiently.
What is Database?
•. In database, different collective information (or data) can be stored.

Hh
Use of Databases.
.
SQL vs NoSQL
The five critical differences of SQL vs NoSQL:
.
1. SQL databases are relational, NoSQL are non-relational.
2. SQL databases use structured query language and have a predefined schema. NoSQL databases have dynamic
schemas for unstructured data.
3. SQL databases are vertically scalable, NoSQL databases are horizontally scalable.
4. SQL databases are table based, while NoSQL databases are document, key-value, graph or wide-column stores.
5. SQL databases are better for multi-row transactions, NoSQL are better for unstructured data like documents or
JSON.
Basic SQL Queries
.
Some of the basic SQL Queries are listed below –

● SELECT - extracts data from a database


● UPDATE - updates data in a database
● DELETE - deletes data from a database
● INSERT INTO - inserts new data into a database
● CREATE DATABASE - creates a new database
● ALTER DATABASE - modifies a database
● CREATE TABLE - creates a new table
● ALTER TABLE - modifies a table
● DROP TABLE - deletes a table
● CREATE INDEX - creates an index (search key)
● DROP INDEX - deletes an index
CRUD Operations.
•. In computer programming, the acronym CRUD stands for create,
read, update and delete.
• These are the four basic or common functions that are executed in
each databases.
• It can also describe user-interface conventions.
• It allows viewing, searching and modifying information through
computer-based forms and reports.
MongoDB.
•. MongoDB is a general purpose, document-based, distributed database built
for modern application developers and for the cloud era.
• MongoDB is a document database, which means it stores data in JSON-like
documents.
• It is much more expressive and powerful than the traditional row/column
model.
• The official website is - https://www.mongodb.com/
• It is one of the “NoSQL” Database.
• It is written in C++ Programming language.
• To download –
https://www.mongodb.com/try/download/community
MongoDB.
.
MongoDB.
.
MongoDB.
.
MongoDB.
.
Robomongo.
•. The Robo 3T (formerly Robomongo) tool has been acquired by 3T
Software Labs - the creators of the MongoDB client Studio 3T
(formerly MongoChef).
• Whatever platform you use today — Robo 3T (formerly
Robomongo) is available for you.
• Distributed as a native application, fast and snappy Robo 3T uses
very little of your machine resources.
• The official website is - https://robomongo.org/
• GitHub repository - https://github.com/Studio3T/robomongo
• Free and open source, forever! – Written in C++.
Robomongo.
•. To download - https://robomongo.org/download
• Now, install it and after installation, click on this “Connect” button.
Node MongoDB Native.
•. The official MongoDB Node.js driver allows Node.js applications
to connect to MongoDB and work with data
• The GitHub Repository -
https://github.com/mongodb/node-mongodb-native
• Documentation Link -
http://mongodb.github.io/node-mongodb-native/
• It’s a MongoDB driver for Node.js
• The MongoDB Node.js core driver is the new underpinning for the
driver.
• It is meant for framework developers that do not need the helpers
and abstractions available in the full driver.
MongoDB - ObjectID.
•. MongoDB uses this “ObjectID” as the default value of “_id” field
for each and every document created here.
• ObjectID is a 12-byte by value, which is auto-generated.
• The first four bytes are a timestamp.
• The next three bytes are a machine identifier.
• The next two bytes are the process ID.
• The last three bytes are the counter, similar to MySQL.
Object Restructuring and De-structuring
•. It lets you pull out properties from an object creating variables.
• If we have an object, say, “stud” and it set equal to an object with
some properties set to some values – like –
⮚ var stud = { name: “Anik”, age: 30}; // Object Re-structuring
• We can pull out one of these into a variables.
• So, to do that – simply –
⮚ var {name} = stud; - This is called object de-structuring.
• Example –
⮚ const { MongoClient } = require('mongodb');
MongoDB - Cursor.
•. It is a part of MongoDB Collection which returns upon the find method
execution.
• It is executed as a loop automatically, by default.
• When we call a find method, all the documents which are returned are saved in
a virtual cursor.
• The official webpage :-
https://docs.mongodb.com/manual/reference/method/js-cursor/
• Different cursors like –
⮚ count()
⮚ forEach()
⮚ pretty()
⮚ toArray()
⮚ sort() and many more …
MongoDB - Client.
•. use DATABASE_NAME is used to create database.
⮚ For example – use boardinfinity
• To switch to any particular “db”, type “use” command.
⮚ For example – use boardinfinity
• To check the current database name, type “db” command.
⮚ db
• To insert any document,
⮚ db.stud.insert({“name”: “Anik”})
• To check all database name:-
⮚ show dbs
MongoDB - Client.
•. To drop / delete a database,
⮚ db.dropDatabase()
• First, we need to go to any particular database, using “use”
command and then use this above function.
• To create a collection, we need to use -
⮚ db.createCollection(“name”)
• To show all existing collections in a database, use -
⮚ “show collections” command
• To drop any collection -
⮚ db.collection_name.drop()
MongoDB - Client.
•. To insert any documents, we can do it in three (3) different ways -
⮚ insert()
⮚ insertOne()
⮚ insertMany()
• insert() function –
⮚ db.stud.insert({“name”: “Anik”})
• insertOne() function
⮚ db.stud.insertOne({“name”: “Anik”, “age”: 30})
• insertMany() function –
⮚ db.stud.insertMany(
[ { name: ‘Ram', courses: 'FSD' }, { name: ‘Sam', courses:
'FSD' } ]
)
MongoDB - Client.
• To find document -
. ⮚ db.collection_name.find()
• To display the data in proper format -
⮚ db.collection_name.find().pretty()
• To display only single document,
⮚ db.collection_name.findOne()
• Different operations can be applied in MongoDB –
⮚ Less Than - db.stud.find({“marks":{$lt:70}})
⮚ Less Than Equal To - db.stud.find({“marks":{$lte:70}})
⮚ Greater Than - db.stud.find({“marks":{$gt:70}})
⮚ Greater Than Equal To - db.stud.find({“marks":{$gte:50}})
⮚ Not equal - db.stud.find({“marks":{$ne:70}})
⮚ Value Present - db.stud.find({"name":{$in:[“Sam", "Ram", “Lucky"]}})
⮚ Value Not Present - db.stud.find({"name":{$nin:[“Sam", "Ram", “Lucky"]}})
MongoDB - Client.
•. To update any documents, we can do it in four (4) different ways -
⮚ update()
⮚ updateOne()
⮚ updateMany()
⮚ findOneAndUpdate()
• To remove any document -
⮚ db.stud.remove({“name”: “Anik”})
• To remove all document -
⮚ db.stud.remove({})
Mongoose ORM.
•. Mongoose ORM stands for Object Relational Mapping, which is a fantastic NPM
library.
• It makes it easy to structure your own desired data.
• For example, to do different types of validation in MongoDB, we can use
“Mongoose”.
• Using Mongoose, we can have built-in automatic validation of the data which you
are inserting/updating.
• The official website is - https://mongoosejs.com/
• Command to install – npm install mongoose --save.

You might also like