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

MongoDB

Uploaded by

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

MongoDB

Uploaded by

Vinoth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

MongoDB and Mongoose JS

with Node.js
- VINOTH S P
Intro.,
• Mongodb is a document-oriented NoSQL database used for high
volume data storage.
• MongoDB is the document database. A document is the JSON
content.
• Relational databases were not designed to cope with the scale and
agility challenges that face modern applications.
Key Points
• Instead tables ( in SQL) we are using documents(in NO SQL).
• These documents are structured in BSON(BSON is the binary encoding of
JSON-like documents that MongoDB uses when storing documents in
collections. BSON supports date and binary also).
• We are not able to use (table) join in No SQL. However references to link
between our (No SQL)documents.
• It is no sequel or non relational database.
• Advantages: Easy to make iterations on schema.
• No SQL is more scalable and provide better performance.
• It accommodate large volume of rapidly changing structured, semi
structured and unstructured data.(we can change the structure anytime we
can able to add the new fields whenever required)
NoSQL Database Types

• Document databases pair each key with a complex data structure known as
a document. Documents can contain many different key-value pairs, or
key-array pairs, or even nested documents.
• Graph stores are used to store information about networks of data, such as
social connections. Graph stores include Neo4J and Giraph.
• Key-value stores are the simplest NoSQL databases. Every single item in the
database is stored as an attribute name (or 'key'), together with its value..
• Wide-column stores such as Cassandra and HBase are optimized for
queries over large datasets, and store columns of data together, instead of
rows.
Replication
• Most NoSQL databases also support automatic database replication
to maintain availability in the event of outages or planned
maintenance events.
• More sophisticated NoSQL databases are fully self-healing, offering
automated failover and recovery, as well as the ability to distribute
the database across multiple geographic regions to withstand regional
failures and enable data localization.
SQL Databases NoSQL Databases

Many different types including


One type (SQL database) with key-value stores, document
Types
minor variations databases, wide-column stores, and
graph databases

Developed in late 2000s to deal


with limitations of SQL databases,
Developed in 1970s to deal with
especially scalability,
Development History first wave of data storage
multi-structured data,
applications
geo-distribution and agile
development sprints.

MySQL, Postgres, Microsoft SQL


Examples MongoDB, Cassandra, HBase, Neo4j
Server, Oracle Database

Varies based on database type. For


example, key-value stores function
similarly to SQL databases, but have
Individual records are stored as
only two columns ('key' and 'value'),
rows in tables, with each column
Data Storage Model Document databases do away with
storing a specific piece of data
the table-and-row model
about that record.
altogether, storing all relevant data
Structure and data types are fixed
in advance. To store information Typically dynamic, with some
about a new data item, the entire enforcing data validation rules.
Schemas
database must be altered, during Applications can add new fields on
which time the database must be the fly.
taken offline.

Vertically, meaning a single server


must be made increasingly
powerful in order to deal with Horizontally, meaning that to add
increased demand. It is possible to capacity, a database administrator
spread SQL databases over many can simply add more commodity
Scaling
servers, but significant additional servers or cloud instances. The
engineering is generally required, database automatically spreads
and core relational features such as data across servers as necessary.
JOINs, referential integrity and
transactions are typically lost.
Mix of open technologies (e.g.,
Development Model Postgres, MySQL) and closed Open technologies
source (e.g., Oracle Database)

Mostly no. MongoDB 4.0 and


Supports multi-record ACID
Yes beyond support multi-document
transactions
ACID transactions. Learn more

Specific language using Select,


Data Manipulation Insert, and Update statements, e.g. Through object-oriented APIs
SELECT fields FROM table WHERE…

Depends on product. Some provide


strong consistency (e.g., MongoDB,
Can be configured for strong
Consistency with tunable consistency for reads)
consistency
whereas others offer eventual
consistency (e.g., Cassandra).
Documents vs collections
• Document is a record in mongodb collection and the basic unit of
data in the mongodb. Document look like JSON objects but exists as
BSON(Binary encoding of JSON).
Documents vs collections
• Collections are the grouping of mongodb document. Typically, all the
document in the collection have a similar or related purpose.
CMDS
• Show dbs – Shows all the databases
• Use <db_name> -- switch to the database
• Show collections – Show all the collections in the current database
• Schema is the representation of collection
• The below objectId key auto generated each time of new document
created.
CMDS
•{
firstname :”Vinoth”
lastname: “S P”
}
db.<Collection_name>.find() 🡪 shows the BSON document
db. <Collection_name>.pretty() 🡪 shows the BSON document with
indent.
• To Create a Collection
• db.createCollection(“<Collection_name>”);
CMD
• Db.NewCollection.remove({name:Vinoth S})
• Removes the document from the collection
• Db.NewCollection.remove()
• Remove all the documents in the collection.
• We can able to create collection using javascript(loop) code in shell.
Schema forms the representation or model encoding of data that gets stored in the
collection.Below are the different types of the data.
Other Types
• db.student.insert({
• name: 'Joe',
• undergrad: true,
• units: 9,
• classes: ['geography', 'math', 'journalism']
• })
• db.student.insert({
• name: 'Jane',
• undergrad: false,
• units: 12,
• classes: ['geography', 'science', 'journalism', 'history']
• })
• db.student.insert({
• name: 'Kevin',
• undergrad: true,
• units: 3,
• classes: ['geography']
• })
• db.student.insert({
• name: 'Rachel',
• undergrad: false,
• units: 6,
• classes: ['geography', 'history']
• })
CMD
• db.student.find({}) //To find all the document in a collection
• db.student.find({'name': 'Rachel’})// To find doc. with name Rachel in
//collection
• db.student.find({units: {$gt: 6}})//filter the documents with the unit value > 6
• db.student.find({units: {$lt: 7}}) //filter the documents with the unit value <6
• db.student.find({classes: {$in: ['history']}}) // retrieve details of class history
• db.student.find({classes: {$in: ['history']}}).sort({units: -1}) // ascending(Unit)
• db.student.find({}).sort({name: 1}) // descending(Name)
• db.student.find({}).sort({name: 1}).limit(2)// restrict the no of row fetched by
//limit
Mongoose
• It is node module, which is used interact with the MongoDB.
• It provides abstraction in the form of API for creating the schemas and
manipulating data.
• Schema creates the key value pair for the different datatypes.
• Mongoose creates easier to use object reference for interacting with
MongoDB. Our database gets modelled within our code.
Schema Creations
DB Connection

You might also like