Mongo DB

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Rich with Features…

MongoDB is a general-purpose database, so aside from creating,


reading, updating, and deleting data, it provides most of the features you
would expect from a database management system and many others that
set it apart. These include:
Indexing
MongoDB supports generic secondary indexes and provides unique,
compound, geospatial, and full-text indexing capabilities as well. Secondary
indexes on hierarchical structures such as nested documents and arrays are
also supported and enable developers to take full advantage of the ability to
model in ways that best suit their applications.
Aggregation
MongoDB provides an aggregation framework based on the concept of data
processing pipelines. Aggregation pipelines allow you to build complex
analytics engines by processing data through a series of relatively simple
stages on the server side, taking full advantage of database optimizations.
Special collection and index types
MongoDB supports time-to-live (TTL) collections for data that should
expire at a certain time, such as sessions and fixed-size (capped) collections,
for holding recent data, such as logs. MongoDB also supports partial indexes
limited to only those documents matching a criteria filter in order to increase
efficiency and reduce the amount of storage space required.
File storage
MongoDB supports an easy-to-use protocol for storing large files and
file metadata.

Some features common to relational databases are not present in


MongoDB, notably complex joins. MongoDB supports joins in a very
limited way through use of the $lookup aggregation operator introduced in
the 3.2 release. In the 3.6 release, more complex joins are possible using
multiple join conditions as well as unrelated subqueries. MongoDB’s
treatment of joins were architectural decisions to allow for greater
scalability, because both of those features are difficult to provide
efficiently in a distributed system.

Without Sacrificing Speed


Performance is a driving objective for MongoDB, and has shaped much
of its design. It uses opportunistic locking in its WiredTiger storage
engine to maximize concurrency and throughput. It uses as much RAM
as it can as its cache and attempts to automatically choose the correct
indexes for queries. In short, almost every aspect of MongoDB was
designed to maintain high performance.

Although MongoDB is powerful, incorporating many features from


relational systems, it is not intended to do everything that a relational
database does. For some functionality, the database server offloads
processing and logic to the client side (handled either by the drivers or
by a user’s application code). Its maintenance of this streamlined design
is one of the reasons MongoDB can achieve such high performance.

Difference between MongoDB and RDBS


A document is a representation of a single entity of data in the
MongoDB database. A collection is made up of one or more related
objects. A major difference between MongoDB and SQL is that
documents are different from rows. Row data is flat, meaning there
is one column for each value in the row. However, in MongoDB,
documents can contain embedded subdocuments, thus providing a
much closer inherent data model to your applications.

Identifying Indexing, Sharding, and Replication Opportunities

MongoDB provides several mechanisms to optimize performance, scaling, and


reliability. As you contemplate your database design, consider each of the
following options:

Indexing: Indexes improve performance for frequent queries by building a


lookup index that can be easily sorted. The _id property of a collection is
automatically indexed on since it is a common practice to look items up by ID.
However, you also need to consider what other ways users access data and
implement indexes that will enhance those lookup methods.

Sharding: Sharding is the process of slicing up large collections of data that


can be split between multiple MongoDB servers in a cluster. Each MongoDB
server is considered a shard. This provides the benefit of using multiple servers
to support a high number of requests to a large system, thus providing
horizontal scaling to your database. Look at the size of your data and the
amount of requests that will be accessing it to determine whether and how
much to shard your collections.

Replications: Replication is the process of duplicating data on multiple


MongoDB instances in a cluster. When considering the reliability aspect of your
database, you should implement replication to ensure that a backup copy of
critical data is always readily available.
Finding Documents in a Collection
Most of the time you use a library such as the native MongoDB driver or
Mongoose to access documents in a collection. However, sometimes you might
need to look at documents inside the MongoDB shell.

use testDB
coll = db.getCollection("newCollection")
coll.find()
coll.find({speed:"120mph"})

Understanding Mongoose
Mongoose is an Object Document Model (ODM) library that wraps around the
MongoDB Node.js driver. The main purpose is to provide a schema-based
solution to model data stored in the MongoDB database.

The chief benefits of using Mongoose are:

 You can create a schema structure for your documents.

  Objects/documents in the model can be validated.

  Application data can be typecast into the object model.

  Business logic hooks can be applied using middleware.

  Mongoose is in some ways easier to use than the MongoDB Node.js


native driver.

However, there are some downsides to using Mongoose as well. Those


drawbacks are:

 You are required to provide a schema, which isn’t always the best option
when MongoDB doesn’t require it.
 It doesn’t perform certain operations, such as storing data, as well as the
native driver does.
Implementing Express in Node.js
Express is a lightweight module that wraps the functionality of the
Node.js http module in a simple to use interface. Express also extends the
functionality of the http module to make it easy for you to handle server routes,
responses, cookies, and statuses of HTTP requests. This chapter gets you
started implementing Express as the webserver for your Node.js applications.
You learn how to configure the Express server, design routes, and use
the Request and Response objects to send and receive HTTP requests. You also
get a look at how to implement template engines in Express.

You might also like