What Is Mongodb?: Unit-5
What Is Mongodb?: Unit-5
What Is Mongodb?: Unit-5
What Is MongoDB?
MongoDB is a document database with the scalability and flexibility that you want with the
querying and indexing that you need.
MongoDB is a source-available cross-platform document-oriented
database program. Classified as a NoSQL database program, MongoDB
uses JSON-like documents with optional schemas. MongoDB is developed
by MongoDB Inc. and licensed under the Server Side Public License (SSPL).
Main features:
Ad-hoc queriesEdit
MongoDB supports field, range query, and regular-expression searches.[28] Queries can
return specific fields of documents and also include user-defined JavaScript functions.
Queries can also be configured to return a random sample of results of a given size.
IndexingEdit
Fields in a MongoDB document can be indexed with primary and secondary indices
or index.
ReplicationEdit
MongoDB provides high availability with replica sets.[29] A replica set consists of two
or more copies of the data. Each replica-set member may act in the role of primary or
secondary replica at any time. All writes and reads are done on the primary replica by
default. Secondary replicas maintain a copy of the data of the primary using built-in
replication. When a primary replica fails, the replica set automatically conducts an
election process to determine which secondary should become the primary.
Secondaries can optionally serve read operations, but that data is only eventually
consistent by default.
If the replicated MongoDB deployment only has a single secondary member, a
separate daemon called an arbiter must be added to the set. It has a single
responsibility, which is to resolve the election of the new primary. [30] As a
consequence, an idealized distributed MongoDB deployment requires at least three
separate servers, even in the case of just one primary and one secondary. [30]
Load balancingEdit
MongoDB scales horizontally using sharding.[31] The user chooses a shard key, which
determines how the data in a collection will be distributed. The data is split into
ranges (based on the shard key) and distributed across multiple shards. (A shard is a
master with one or more replicas.). Alternatively, the shard key can be hashed to map
to a shard – enabling an even data distribution.
MongoDB can run over multiple servers, balancing the load or duplicating data to keep
the system up and running in case of hardware failure.
File storageEdit
MongoDB can be used as a file system, called GridFS, with load balancing and data
replication features over multiple machines for storing files.
This function, called grid file system,[32] is included with MongoDB drivers. MongoDB
exposes functions for file manipulation and content to developers. GridFS can be
accessed using mongofiles utility or plugins for Nginx[33] and lighttpd.[34] GridFS
divides a file into parts, or chunks, and stores each of those chunks as a separate
document.[35]
AggregationEdit
MongoDB provides three ways to perform aggregation: the aggregation pipeline, the
map-reduce function, and single-purpose aggregation methods. [36]
Map-reduce can be used for batch processing of data and aggregation operations.
But according to MongoDB's documentation, the Aggregation Pipeline provides
better performance for most aggregation operations.[37]
The aggregation framework enables users to obtain the kind of results for which
the SQL GROUP BY clause is used. Aggregation operators can be strung together to
form a pipeline – analogous to Unix pipes. The aggregation framework includes the
$lookup operator which can join documents from multiple collections, as well as
statistical operators such as standard deviation.
Server-side JavaScript executionEdit
JavaScript can be used in queries, aggregation functions (such as MapReduce), and
sent directly to the database to be executed.
Capped collectionsEdit
MongoDB supports fixed-size collections called capped collections. This type of
collection maintains insertion order and, once the specified size has been reached,
behaves like a circular queue.
TransactionsEdit
MongoDB claims to support multi-document ACID transactions since the 4.0 release
in June 2018.[38] This claim was found to not be true as MongoDB violates snapshot
isolation.
Architecture:
Programming language accessibilityEdit
MongoDB has official drivers for major programming languages and development
environments.[43] There are also a large number of unofficial or community-
supported drivers for other programming languages and frameworks.
Serverless accessEdit
Management and graphical front-endsEdit
The primary interface to the database has been the mongo shell. Since MongoDB 3.2,
MongoDB Compass is introduced as the native GUI. There are products and third-
party projects that offer user interfaces for administration and data viewing. [44]
In MongoDB, you can search by field, range query and it also supports regular expression
searches.
2. Indexing
3. Replication
A master can perform Reads and Writes and a Slave copies data from the master and
can only be used for reads or back up (not writes)
4. Duplication of data
MongoDB can run over multiple servers. The data is duplicated to keep the system
up and also keep its running condition in case of hardware failure.
5. Load balancing
10. Stores files of any size easily without complicating your stack.
Now a day many companies using MongoDB to create new types of applications,
improve performance and availability.
Create Database
Use Database method:
There is no create database command in MongoDB. Actually, MongoDB do not provide any
command to create database.
It may be look like a weird concept, if you are from traditional SQL background where you
need to create a database, table and insert values in the table manually.
Here, in MongoDB you don't need to create a database manually because MongoDB
will create it automatically when you save the value into the defined collection at first
time.
Syntax:
1. use DATABASE_NAME
1. >use javatpointdb
Database
In MongoDB, a database contains the collections of documents. One can create
multiple databases on the MongoDB server.
View Database:
To see how many databases are present in your MongoDB server, write the following
statement in the mongo shell:
show dbs
aming Restriction for Database:
Before creating a database you should first learn about the naming
restrictions for databases:
In MongoDB, the names of the database are case insensitive, but you
must always remember that the database names cannot differ only by the
case of the characters.
For windows user, MongoDB database names cannot contain any of
these following characters:
/\. "$*:|?
For Unix and Linux users, MongoDB database names cannot contain any
of these following characters:
/\. "$
MongoDB database names cannot contain null characters(in windows,
Unix, and Linux systems).
MongoDB database names cannot be empty and must contain less than
64 characters.
Creating Database:
In the mongo shell, you can create a database with the help of the following
command:
use database_name
This command actually switches you to the new database if the given name
does not exist and if the given name exists, then it will switch you to the
existing database. Now at this stage, if you use the show command to see
the database list where you will find that your new database is not present in
that database list because, in MongoDB, the database is actually created
when you start entering data in that database.
Collections:
Collections are just like tables in relational databases, they also store data,
but in the form of documents. A single database is allowed to store multiple
collections.
Schemaless:
Before creating a collection you should first learn about the naming
restrictions for collections:
Collection name must starts with an underscore or a character.
Collection name does not contain $, empty string, null character and does
not begin with system. prefix.
The maximum length of the collection name is 120 bytes(including the
database name, dot separator, and the collection name).
Creating collection:
Before moving further first you should learn about the naming restrictions for
fields:
The field names are of strings.
The _id field name is reserved to use as a primary key. And the value of
this field must be unique, immutable, and can be of any type other than
an array.
The field name cannot contain null characters.
The top-level field names should not start with a dollar sign ($).
Document Size: The maximum size of the BSON document is 16MB. It
ensures that the single document does not use too much amount of RAM or
bandwidth(during transmission). If a document contains more data than the
specified size, then MongoDB provides a GridFS API to store such type of
documents.
Important Notes –
A single document may contain duplicate fields.
MongoDB always saves the order of the fields in the documents except
for the _id field (which always comes in the first place) and the renaming
of fields may change the order of the fields in the documents.
_id Field: In MongoDB, every document store in the collection must
contain a unique _id field it is just like a primary key in a relational
database. The value of the _id field can be set by the user or by the
system (if the user does not create an _id field, then the system will
automatically generate an ObjectId for _id field).
When you create a collection MongoDB automatically creates a
unique index on the _id field.
The _id field is the first field of every document.
The value of the _id field can be of any BSON type except
arrays.
The default value of the _id field is ObjectId.
Application Deployment
MongoDB Realm uses a deployment-based versioning scheme that allows you to
group updates to your application and make them available to client applications at
the same time. You can deploy a new version of your application with multiple
deployment methods that each fit a particular development workflow. You can also
review the historical state of your application based on the built-in deployment history
log.
Deployment Methods
You can deploy new versions of your application through the Realm UI or one of multiple
code-based deployment methods that each suit a particular development workflow.
You want to use a browser-based GUI to develop and manage your Realm app.
Deployment Pattern
1. Make changes in the UI that are saved in a draft state and not visible to client apps.
2. Review all draft changes and then deploy them together through the Realm UI.
Deployment Pattern
Deployment Method Details
Deployment Pattern
Deployment Pattern
Application Components
Application Directory
Realm apps are structured in a nested directory format called an application directory that
contains configuration files for your application components. The configuration files are
grouped into directories based on their component type. You can download a copy of the
application directory for the most recently deployed version of an app by exporting it from
the Realm UI or realm-cli , or by cloning the underlying Git repository.
For additional information on the structure of an application directory as well as the schema
of each configuration file type, see Realm Application Configuration.
Deployment History
MongoDB Realm logs every deployment of an application and exposes a list of the
25 most recent deployments in the History tab of the Deploy in the Realm UI. Each
deployment log includes data about a set of changes, such as their status, origin,
and time of deployment.
If you choose to deploy through the Realm UI, any changes you make are grouped
in a row at the top of the deployment history table that represents draft changes
made since the previous deployment. You can use the Action buttons on this row to
manually deploy all draft changes.
Cloud deployment can be viewed from the angle of management responsibility for
the deployment of the SaaS, PaaS and/or IaaS solutions in question. From this
perspective, there are two possible approaches: the cloud solution(s) may be
deployed by a third party (under a community cloud, public cloud or private cloud
deployment model) or the cloud solution(s) may be deployed by a single entity
(under a private cloud deployment model).
After cloud deployment has been completed for a SaaS, PaaS or IaaS solution, user
provisioning can occur based on user permissions, where access is provided for
cloud resources based on the consumer’s classification as either a trusted or
untrusted entity. Trusted entities may receive access permission to managed cloud,
private cloud or hybrid cloud resources. Untrusted entities may receive access
permission to public cloud, managed cloud or hybrid cloud resources. The key
difference between trusted and untrusted entities is that untrusted entities never
receive access permission to private cloud resources.