Mongodb
Mongodb
Introduction
1 Article
Learn
Installing MongoDB
1 Article
Learn
4 Articles
Learn
Mongoose
2 Articles
Learn
9 Articles
Learn
Operators in MongoDB
5 Articles
Learn
1 Article
Introduction to Node.js - Fork MongoDB
Introduction:
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code
outside a browser. You need to remember that NodeJS is not a framework and it's not a
programming language. Most people are confused and understand it's a framework or a programming
language. We often use Node.js for building back-end services like APIs, Web Apps, or Mobile Apps. It's
used in production by companies such as Paypal, Uber, Netflix, Walmart, and many others.
Features of Node.js:
There are other programming languages also that can be used to build back-end services so what makes
Node.js different?
1. It's easy to get started and can be used for prototyping and agile development
2. It provides fast and highly scalable services
3. It uses JavaScript under the hood, so it's easy for a JavaScript programmer to build back-end
services using Node.js
4. Source code is cleaner and consistent.
5. Large ecosystem for open source library.
6. It is Asynchronous or Non-blocking in nature.
Advantages of Node.js:
1. Easy Scalability: Developers prefer to use Node.js because it is easily scalable. We can also add
extra resources during the scalability of the application.
2. Real-time web apps: If you are building a web app you can also use PHP, and it will take the
same amount of time as you use Node.js, But if I am talking about building chat apps or gaming
apps Node.js is much more preferable because of faster synchronization. Also, the event loop
avoids HTTP overloading for Node.js development.
3. Fast Suite: NodeJs runs on the V8 engine developed by Google. The event loop in NodeJs handles
all asynchronous operations so NodeJs acts like a fast suite and all the operations can be done
quickly like reading or writing in the database, network connection, or file system
4. Easy to learn and code: NodeJs is easy to learn and code because it uses JavaScript. If you are a
front-end developer and have a good grasp of JavaScript you can easily learn and build the
application on NodeJS
5. Advantage of Caching: It provides the caching of a single module. Whenever there is any
request for the first module, it gets cached in the application memory, so you don't need to re-
execute the code.
6. Data Streaming: In NodeJs HTTP request and response are considered as two separate events.
They are data streams so when you process a file at the time of loading it will reduce the overall
time and will make it faster when the data is presented in the form of transmissions. It also
allows you to stream audio and video files at lightning speed.
7. Hosting: PaaS (Platform as a Service) and Heroku are the hosting platforms for NodeJS
application deployment which is easy to use without facing any issue.
8. Corporate Support: Most of the well-known companies like Walmart, Paypal, Microsoft, Yahoo
are using NodeJS for building the applications.
Application of NodeJS:
Real-Time Chats,
Complex Single-Page applications,
Real-time collaboration tools,
Streaming apps
JSON APIs based application
Using Website:
1. You can visit the link Download Node and download LTS version.
2. After installing the node you can check your node version in the command prompt using command..
~ $node --version
3. After that, you can just create a folder and add a file here for example app.js. To run this file you need
to execute the command…
4. Node Modules: There are some built-in modules that you can use to create your applications. Some
popular modules are- OS, fs, events, HTTP, and URL, and then you can include these modules in your
file using these lines.
var fs = require('fs');
javascript
This will listen to the server on port 8080. Once you will run your file in the command prompt it will
execute your file and listen to the server on this port. You can also create your own module and include
it in your file.
Using NPM: NPM is a Node Package Manager that provides packages to download and use. It
contains all the files and modules that you require in your application. To install any package
you need to execute a command...
npm install
javascript
console.log('Welcome to GeeksforGeeks');
}
So this is how you can start with Node.js and build your own applications. There are some frameworks
of the node which you can use to build your applications. Some popular frameworks of node
are...Express.js, Socket.io, Koa.js, Meteor.js, Sail.js.
Features:
Step 3: There you will find the Available Download option, you may have to give your platform, then
click Download.
Step 4: Then you have to install the downloaded file.
Step 5: Then you have to agree to the terms & conditions.
Step 10: Then go to the command prompt. There you have to type the following two commands.
mongo
show dbs
Then you will find MongoDB enterprise is installed properly. Hence your installation is successful.
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term
'NoSQL' means 'non-relational'. It means that MongoDB isn't based on the table-like relational database
structure but provides an altogether different mechanism for storage and retrieval of data. This format
of storage is called BSON (Binary Encoded Javascript Notation) ( similar to JSON format).
SQL databases store data in tabular format. This data is stored in a predefined data model which is not
very much flexible for today's real-world highly growing applications. Modern applications are more
networked, social and interactive than ever. Applications are storing more and more data and are
accessing it at higher rates.
Relational Database Management System(RDBMS) is not the correct choice when it comes to
handling big data by the virtue of their design since they are not horizontally scalable. If the
database runs on a single server, then it will reach a scaling limit. NoSQL databases are more scalable
and provide superior performance. MongoDB is such a NoSQL database that scales by adding more and
more servers and increases productivity with its flexible document model.
RDBMS vs MongoDB:
RDBMS has a typical schema design that shows a number of tables and the relationship between
these tables whereas MongoDB is document-oriented. There is no concept of schema or
relationship.
Complex transactions are not supported in MongoDB because complex join operations are not
available.
MongoDB allows a highly flexible and scalable document structure. For example, one data
document of a collection in MongoDB can have two fields whereas the other document in the
same collection can have four.
MongoDB is faster as compared to RDBMS due to efficient indexing and storage techniques.
There are a few terms that are related in both databases. What's called a Table in RDBMS is called
a Collection in MongoDB. Similarly, a Tuple is called a Document and A Column is called a Field.
MongoDB provides a default '_id' (if not provided explicitly) which is a 12-byte hexadecimal
number that assures the uniqueness of every document. It is similar to the Primary key in
RDBMS.
Features of MongoDB:
Document Oriented: MongoDB stores the main subject in the minimal number of documents
and not by breaking it up into multiple relational structures like RDBMS. For example, it stores
all the information of a computer in a single document called Computer and not in distinct
relational structures like CPU, RAM, Hard disk, etc.
Indexing: Without indexing, a database would have to scan every document of a collection to
select those that match the query which would be inefficient. So, for efficient searching Indexing
is a must and MongoDB uses it to process huge volumes of data in very less time.
Scalability: MongoDB scales horizontally using sharding (partitioning data across various
servers). Data is partitioned into data chunks using the shard key, and these data chunks are
evenly distributed across shards that reside across many physical servers. Also, new machines
can be added to a running database.
Replication and High Availability: MongoDB increases the data availability with multiple
copies of data on different servers. By providing redundancy, it protects the database from
hardware failures. If one server goes down, the data can be retrieved easily from other active
servers which also had the data stored on them.
Aggregation: Aggregation operations process data records and return the computed results. It
is similar to the GROUPBY clause in SQL. A few aggregation expressions are sum, avg, min, max,
etc
Big Data: If you have huge amount of data to be stored in tables, think of MongoDB before
RDBMS databases. MongoDB has built-in solution for partitioning and sharding your database.
Unstable Schema: Adding a new column in RDBMS is hard whereas MongoDB is schema-less.
Adding a new field does not effect old documents and will be very easy.
Distributed data Since multiple copies of data are stored across different servers, recovery of
data is instant and safe even if there is a hardware failure.
Language Support by MongoDB:
MongoDB currently provides official driver support for all popular programming languages like C, C++,
Rust, C#, Java, Node.js, Perl, PHP, Python, Ruby, Scala, Go, and Erlang.
Installing MongoDB:
For Windows, a few options for the 64-bit operating systems drops down. When you're running on
Windows 7, 8 or newer versions, select Windows 64-bit 2008 R2+. When you're using Windows XP
or Vista then select Windows 64-bit 2008 R2+ legacy.
MongoDB has been adopted as backend software by a number of major websites and services including
EA, Cisco, Shutterfly, Adobe, Ericsson, Craigslist, eBay, and Foursquare.
A NoSQL originally referring to non SQL or non relational is a database that provides a mechanism for
storage and retrieval of data. This data is modeled in means other than the tabular relations used in
relational databases. Such databases came into existence in the late 1960s, but did not obtain the NoSQL
moniker until a surge of popularity in the early twenty-first century. NoSQL databases are used in real-
time web applications and big data and their use are increasing over time. NoSQL systems are also
sometimes called Not only SQL to emphasize the fact that they may support SQL-like query languages.
A NoSQL database includes simplicity of design, simpler horizontal scaling to clusters of machines and
finer control over availability. The data structures used by NoSQL databases are different from those
used by default in relational databases which makes some operations faster in NoSQL. The suitability
of a given NoSQL database depends on the problem it should solve. Data structures used by NoSQL
databases are sometimes also viewed as more flexible than relational database tables.
Many NoSQL stores compromise consistency in favor of availability, speed and partition tolerance.
Barriers to the greater adoption of NoSQL stores include the use of low-level query languages, lack of
standardized interfaces, and huge previous investments in existing relational databases. Most NoSQL
stores lack true ACID(Atomicity, Consistency, Isolation, Durability) transactions but a few databases,
such as MarkLogic, Aerospike, FairCom c-treeACE, Google Spanner (though technically a NewSQL
database), Symas LMDB, and OrientDB have made them central to their designs.
Most NoSQL databases offer a concept of eventual consistency in which database changes are
propagated to all nodes so queries for data might not return updated data immediately or might result
in reading data that is not accurate which is a problem known as stale reads. Also some NoSQL systems
may exhibit lost writes and other forms of data loss. Some NoSQL systems provide concepts such as
write-ahead logging to avoid data loss. For distributed transaction processing across multiple
databases, data consistency is an even bigger challenge. This is difficult for both NoSQL and relational
databases. Even current relational databases do not allow referential integrity constraints to span
databases. There are few systems that maintain both X/Open XA standards and ACID transactions for
distributed transaction processing.
Advantages of NoSQL:
There are many advantages of working with NoSQL databases such as MongoDB and Cassandra. The
main advantages are high scalability and high availability.
1. High scalability - NoSQL database use sharding for horizontal scaling. Partitioning of data and
placing it on multiple machines in such a way that the order of the data is preserved is sharding.
Vertical scaling means adding more resources to the existing machine whereas horizontal
scaling means adding more machines to handle the data. Vertical scaling is not that easy to
implement but horizontal scaling is easy to implement. Examples of horizontal scaling databases
are MongoDB, Cassandra etc. NoSQL can handle huge amount of data because of scalability, as
the data grows NoSQL scale itself to handle that data in efficient manner.
2. High availability - Auto replication feature in NoSQL databases makes it highly available
because in case of any failure data replicates itself to the previous consistent state.
Disadvantages of NoSQL:
1. Narrow focus - NoSQL databases have very narrow focus as it is mainly designed for storage
but it provides very little functionality. Relational databases are a better choice in the field of
Transaction Management than NoSQL.
2. Open-source - NoSQL is open-source database. There is no reliable standard for NoSQL yet. In
other words two database systems are likely to be unequal.
3. Management challenge - The purpose of big data tools is to make management of a large
amount of data as simple as possible. But it is not so easy. Data management in NoSQL is much
more complex than a relational database. NoSQL, in particular, has a reputation for being
challenging to install and even more hectic to manage on a daily basis.
4. GUI is not available - GUI mode tools to access the database is not flexibly available in the
market.
5. Backup - Backup is a great weak point for some NoSQL databases like MongoDB. MongoDB has
no approach for the backup of data in a consistent manner.
6. Large document size - Some database systems like MongoDB and CouchDB store data in JSON
format. Which means that documents are quite large (BigData, network bandwidth, speed), and
having descriptive key names actually hurts, since they increase the document size.
Types of NoSQL databases and the name of the databases system that falls in that category are:
NoSQL refers to a non SQL or nonrelational database that main purpose of it is to provide a mechanism
for storage and retrieval of data. NoSQL database stores the information in JSON documents instead of
columns and rows. As we know the relational database use rows and columns for storing and retrieval
of data but in the case of NoSQL it uses JSON documents instead of rows and columns and that is why it
is also known as nonrelational SQL or database.
A NoSQL database includes simplicity of design, simpler horizontal scaling, and has fine control over
availability. The data structures used in the NoSQL database are different from those we used in the
relational database. the database used in NoSQL is more advanced which makes some operations faster
in NoSQL.
Figure 1:
Graphical difference between SQL and NoSQL
Relationships present in NoSQL are less complex as compared to relational database systems.
Actions performed in NoSQL are fast as compared to other databases.
Implementation of it is less costly than others.
Programming in it is easy to use and more flexible.
A high level of scalability is provided by NoSQL.
Types of NoSQL:
This is a specific type of NoSQL database in which the key-value method is used and its main purpose is
to represent the various key-value pairs. Here the keys are called unique identifiers for values and
values can represent more than one type of object like - a string or even a string.
This is different from a relational database in such a way, key-value databases do not contain any
defined structure as we know that relational databases represent data into tables and columns by which
relational database has a well-defined structure and its data types are assigned to its columns whereas
key-value database contains a pair of keys and values.
Think about a dictionary such as a dictionary containing various words and their meanings. so here the
word in the dictionary is the key and its meaning as the values. key names can be specified from as
simple as numbers to any specific descriptions of the values.
In the given below example, we have a pair of keys and values as we discussed above as in the on left
there is a column for key and their values are defined on the right of these keys.
Figure 2:
Diagram of Key-Value Store in NoSQL
Consistency: Consistency is a feature only applicable for operations on a single key in a key-
value store. There are various implementations in the key-value store for example in RIAK, the
eventually consistent model of consistency is implemented.
Transactions: In it, there are no guarantees on the writes as many data stores implement
transactions in different ways for example RIAK uses the concept of quorum implemented by
using the W value replication factor. (RIAK is an open-source and distributed database that is
generally based on a NoSQL database system.)
Query: All the key-value stores can be query by the key and that's about it. If we have
requirements to query by using some of the attributes of the column, it is not possible for using
the database in this condition, our application needs to read the value to recognize if the
attribute meets the conditions.
Scaling: Key values stored scale by a process called sharding. Sharding means we can support
scalability by putting different parts of the data onto different servers, this is called sharding.
Popular Key-Value Databases:
REDIS: Redis is one of the popular key-value databases as it is an open-source, in-memory data
structure, used as a database and message broker. REDIS supports many data structures such as
lists, hashes, sets, strings. REDIS has many more important features such as it has built-in
replication, LUA scripting and it also supports LRU eviction.
AEROSPIKE: It is the world's leading enterprise-grade, internet-scale, key-value store database,
it is popular for some of its advantages over other databases such as aerospike gives strong
consistency, linear scalability, and higher performance as compared to others.
AMAZON DynamoDB: The main reason behind the popularity of this database is that it is a fully-
managed database service that provides fast performance at any scale. Many AWS customers
chose DynamoDB for web gaming, mobile, ed-tech, IoT, and many other applications.
MongoDB stores data records as documents that are stored together in collections and the database
stores one or more collections of documents.
Document: A document is a basic unit of storing data into the database. A single record of a collection
is also known as a document. Basically, It is a structure that compromises key & value pairs which is
similar to the JSON objects. Documents have a great ability to store complex data. For example:
Here, the name, country, age and status are fields, and gfg, India, 21, A are their values.
Collection: It is used to store a varied number of documents inside the database. As MongoDB is a
Schema-free database, it can store the documents that are not the same in structure. Also, there is no
need to define the columns and their datatype.
Database: The MongoDB database is a container for collections and it can store one or more
collections. It is not necessary to create a database before you work on it. The show dbs command gives
the list of all the databases.
Creating a Database
In MongoDB, we can create a database using the use command. As shown in the below image
use gfgDB
Here, we have created a database named as "gfgDB". If the database doesn't exist, MongoDB will create
the database when you store any data to it. Using this command we can also switch from on database
to another.
In MongoDB, we can view all the existing database using the following command:
show dbs
Creating a Collection
In MongoDB, a new collection is created when we add one or more documents to it. We can insert
documents in the collection using the following methods:
Here, we create a collection named as myNewCollection1 by inserting a document that contains a name
field with its value in it using insertOne() method.
db.myNewCollection2.insertMany([{name:"gfg", country:"India"},
{name:"rahul", age:20}])
In MongoDB, we can view all the existing collection in the database using the following command:
show collections
This command returns a list of all the existing collections in the gfgDB database.
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term
‘NoSQL’ means ‘non-relational’. MongoDB provides us flexible database schema that has its own
advantages and disadvantages. Every record in MongoDB collections does not depend upon the other
records present in a particular collection on the basis of structure. We can add any new key in any
record according to the need. There is no proper structure for the MongoDB collections and constraints
on the collections. Let's have a look at an example.
MongoDB:
Database: GFG
Collection: GFG1
In the above example, we can easily see there is no proper schema for a collection in MongoDB. We can
use any numbers of different keys and value in the collection. This phenomenon might create some
troubles. So let's see how can we overcome this problem.
Mongoose.module is one of the most powerful external module of the node.js. Mongoose is a MongoDB
ODM (Object database Modelling) that is used to translate the code and its representation from
MongoDB to the Node.js server.
Mongoose module provides several functions in order to manipulate the documents of the collection of
the MongoDB database (Refer this Link)
Project Structure:
Running the server on Local IP: Data is the directory where MongoDB server is present.
Index.js
Javascript
// Database Address
const url = "mongodb://localhost:27017/GFG"
// Connecting to database
mongoose.connect(url).then((ans) => {
console.log("ConnectedSuccessful")
}).catch((err) => {
console.log("Error in the Connection")
})
// Creating collection
const collections = mongoose.model(
"GFG2", collection_structure)
node index.js
Console output:
Mongoose module imposed a definite structure on the collection and makes the collection rigid.
Before we dive into looking how the mongoDB connects with a Nodejs app using mongooseJS, lets get a
brief intro to these technologies which are ruling the web development domain of today.
Node: Node.js (Node) is an open source development platform for executing JavaScript code server-
side. Node is useful for developing applications that require a persistent connection from the browser
to the server and is often used for real-time applications such as chat, news feeds and web push
notifications.
It can be downloaded from here .
NPM: Node Package Manager or NPM is the official package manager for nodeJS applications. It will
come installed with NodeJS. It is used from command line or terminal(depending on what OS is being
used).
So now that we are familiar with what the basic definitions of these technologies, lets dive into the code
and its explanations.
Our nodejs app in this case(for demo purpose) is going to be a single javascript file. Let's call it app.js.
Go ahead and create that file in a new folder.
the source code for our nodejs
application,.
Line 4 : It assigns the connection string (which contains the info about connection to database) to our
mongoDB variable.
Line 6 : These lines help in establishing or 'open' or fire up a connection with the database mentioned
mentioned in the mongoDB variable. The first argument to the mongoose.connect() function is the
connection string(the mongoDB variable).
Line 14 : This line logs the message into the console when the connection to the database has been
made and returned. It listens for 'connected' event and when the event fires, the function() comprising
of line 14,15,16 gets executed.
Line 19 : This line logs the message into the console when the connection to the database has been
made and returned. It listens for 'error' event and when the event fires, the function() comprising of
line 19,20,21 gets executed.
After writing the source code open up a terminal or the command prompt(in case of windows users)
and navigate to your project directory. then write the command npm install mongooose as shown in
the image below:
the command
installs the mongoose package for use in the app
The above command will create a 'node_modules' folder in your current directory or folder and
download the necessary files there.
If you are following until now, all the preparation is done and now we can test our connection to
database.
Write node app.js to start the app.
the node
app.js command runs our application. the db.on('connected') events fires up and the function gets
executed.
Advantage of JSON:
Faster: The XML program parsing handle is slower than JSON. Typically since the DOM control
libraries require more memory to handle huge XML records. JSON, on the other hand,
employments less information which diminishes the taken a toll and increments the parsing
speed.
Structured Data: JSON employments a outline information structure though XML includes a
tree structure. The key or esteem sets can restrain your errand, but you get unsurprising and
easy-to-understand information to demonstrate.
Readable: It is Human-readable and writable. It is light weight text based data interchange
format which means, it is simpler to read and write when compared to XML.
BSON: BSON could be a binary shape of JSON that utilized to speak to information structures - called
records or objects - in MongoDB. In spite of the fact that comparative to JSON, BSON isn't very the same
thing. Plaintext JSON is to some degree wasteful for bulk capacity and information transmission, so
BSON arrangement can be utilized after you got to send a part of JSON information at tall speed - and
don’t need to bargain with compression, which can be expensive. Converting BSON to JSON and vice-
versa is large much speedier than compressing JSON employing a general-purpose compression
calculation.
Advantage of BSON:
Broadly JSON comprises of question and The parallel encoding method comprises of extra data
cluster where the question could be a such as lengths of strings and the protest subtypes. In
collection of key-value sets and the cluster is addition, BinData and Date information sorts are the
requested list of values. information sorts which are not upheld in JSON.
JSON stands for JavaScript Object Notation. BSON stands for Binary JavaScript Object Notation.
JSON BSON
In MongoDB, find() method is used to select documents in a collection and return a cursor to the
selected documents. Cursor means a pointer that points to a document, when we use find() method it
returns a pointer on the selected documents and returns one by one. If we want to return pointer on all
documents then use empty() parameter that returns all documents one by one. It takes only some
optional parameters. The first optional parameter is the selection criteria on which we want to return
a cursor. To return all documents in a collection use empty document({}). Using this method you can
also replace embedded documents. You can also use this method in multi-document transactions. If you
use this method in the mongo shell, then the shell will automatically iterate the cursor to display up to
20 documents in the collection, if you want to continue then type it or you can manually iterate the
result of the find() method by assigning the returned cursor to a variable with the var keyword. You can
also modify the behavior of this method using cursor methods.
Syntax:
db.Collection_name.find(selection_criteria, projection)
Optional parameters:
selection_criteria: It specifies selection criteria. To return all documents in a collection use
empty document({}). The type of this parameter is document.
projection: It specifies the fields to return in the documents that match the selection criteria. To
return all fields in the matching documents, remove this parameter. It is of the document type.
Here if the value of the field is 1/true then it specifies the inclusion of the field, or if the value of the field
is 0/false then it specifies the exclusion of the field.
Return:
It returns a cursor to the documents that match the selection criteria. When the find() method returns
documents, the method is actually returning a cursor to the documents.
Examples:
Database: gfg
Collections: student
db.student.find()
Find all the documents present in the collection by passing empty document:
db.student.find({})
Find all the document that matches the given filter query(i.e., age:18):
db.student.find({age:18})
Find the embedded document that matches the given filter query:
db.student.find({},{name:1, _id:0})
db.student.find().limit(2)
In MongoDB, the insert() method inserts a document or documents into the collection. It takes two
parameters, the first parameter is the document or array of the document that we want to insert and
the remaining are optional.
Using this method you can also create a collection by inserting documents.
You can insert documents with or without _id field. If you insert a document in the collection without _id
field, then MongoDB will automatically add an _id field and assign it with a unique ObjectId. And if you
insert a document with _id field, then the value of the _id field must be unique to avoid the duplicate key
error.
This method can also be used inside multi-document transactions.
Syntax:
db.Collection_name.insert(
writeConcern: <document>,
ordered: <boolean>
})
Parameters:
The first parameter is the document or an array of documents. Documents are a structure created of file
and value pairs, similar to JSON objects.
The second parameter is optional.
Optional Parameters:
writeConcern: It is only used when you do not want to use the default write concern. The type of this
parameter is a document.
ordered: The default value of this parameter is true. If it is true, it inserts documents in the ordered
manner. Otherwise, it randomly inserts documents.
Return:
This method returns WriteResult when you insert single document in the collection.
This method returns BulkWriteResult when you insert multiple documents in the collection.
Examples:
Database: gfg
Collection: student
Document: No document but, we want to insert in the form of the student name and student
marks.
Insert the document whose name is Akshay and marks is 500
Here, we insert a document in the student collection whose name is Akshay and marks is 500 using
insert() method.
Output:
Output:
Output:
MongoDB stores document in BSON, a binary form of JSON (JavaScript Object Notation). The documents
are store in collection.
Syntax:
use collection_name
It will create a collection if it does not exist otherwise it returns the existing collection.
Create a collection
To show the currently selected collection use 'db' command as shown below:
Step 2: Insert Data into Collection Documents in MongoDB can be inserted using two methods:
Syntax:
db.collection_name.insertOne(item:'item1'}) // OR
db.collection_name.insert(item:'item1'})
Example:
Inserting a document without _id field
Here we can specify the _id field if it does not specify then MongoDB adds the _id field with value
objectId.
Syntax:
db.collection_name.insertMany([{item:'item1'}, {item:'item2'},
{item:'item3'}, {item:'item4'}])
The query operation performs using db.collection.find() method in MongoDB. To query documents in
MongoDB follow the steps mentioned below:
Step 1: Create collection and Insert documents using Mongo Shell
Insert
Documents in Collection
Step 2: Select All Documents in a Collection - To select all documents in the collection, pass an empty
document as the query filter parameter to the find method. This statement is similar to the SELECT *
FROM table statement in MySQL.
Syntax:
db.collection_name.find({})
Select All
Documents in a Collection
Step 3: Specify Equality Condition - To filter the result of db.collection_name.find() method you need
to specify conditions for method.
Syntax:
Step 4: Specify Conditions Using Query Operators - A query filter document can use the query
operators to specify a condition.
Syntax:
1. 'in' Operator ($in) - The following example retrieve all documents which status value is "C" or
"B".
Specify Conditions Using 'in' Operator.
2. 'AND' Operator(, ) - A compound query can specify conditions for more than one field in the
collection’s documents. Implicitly, a logical AND conjunction connects the clauses of a compound
query so that the query selects the documents in the collection that match all the conditions.
The following example returns the documents which status is "A" and quantity is less than 540.
'AND'
Operator
3. 'OR' Operator ($or) - Using the $or operator, you can specify a compound query that joins each
clause with a logical OR conjunction so that the query selects the documents in the collection
that match at least one condition.
The following example returns the documents which status is "A" or quantity is less than 540.
'OR'
Operator ($or)
You can use 'AND' and 'OR' operators at a same time, in the following example the query returns
documents which status equals "A" and either qty is less than 540 or item starts with the
character "i".
Using
'AND' and 'OR' same time
The update() method updates the values in the existing document in the collections of MongoDB. When
you update your document the value of the _id field remains unchanged. By default, the
db.collection.update() method updates a single document. Include the option multi: true to update all
documents that match the given query. This method can be used for a single updating of documents as
well as multi documents.
Syntax:
db.COLLECTION_NAME.update({SELECTION_CRITERIA}, {$set:{UPDATED_DATA}}, {
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string>
})
Parameters:
The first parameter is the Older value in the form of Documents. Documents are a structure
created of file and value pairs, similar to JSON objects.
The second parameter must contain a $set keyword to update the following specify document
value.
The third parameter is optional.
Optional Parameters:
Upsert: The default value of this parameter is false. When it is true it will make a new document
in the collection when no document matches the given condition in the update method.
multi: The default value of this parameter is false. When it is true the update method update all
the documents that meet the query condition. Otherwise, it will update only one document.
writeConcern: It is only used when you do not want to use the default write concern. The type
of this parameter is a document.
Collation: It specifies the use of the collation for operations. It allows users to specify the
language-specific rules for string comparison like rules for lettercase and accent marks. The type
of this parameter is a document.
arrayFilters: It is an array of filter documents that indicates which array elements to modify for
an update operation on an array field. The type of this parameter is an array.
hint: It is a document or field that specifies the index to use to support the filter. It can take an
index specification document or the index name string and if you specify an index that does not
exist, then it will give an error.
Examples:
Database: gfg
Collections: student
Document: Three documents contains name and the age of the students
Update the name of the document whose name key has avi value to hello world.
db.student.update({name:"avi"},{$set:{name:"helloword"}})
Here, the first parameter is the document whose value to be changed {name:"avi"} and the second
parameter is set keyword means to set(update) the following matched key value with the older key
value.
Note: The value of the key must be of the same datatype that was defined in the collection.
db.student.update({name:"prachi"},{$set:{age:20}}
Here, the first parameter is the document whose value to be changed {name:"prachi"} and the second
parameter is set keyword means to set(update) the value of the age field to 20.
In MongoDB, upsert is an option that is used for update operation e.g. update(), findAndModify(), etc.
Or in other words, upsert is a combination of update and insert (update + insert = upsert). If the value
of this option is set to true and the document or documents found that match the specified query, then
the update operation will update the matched document or documents. Or if the value of this option is
set to true and no document or documents matches the specified document, then this option inserts a
new document in the collection and this new document have the fields that indicate in the operation.
By default, the value of the upsert option is false. If the value of upsert in a sharded collection is true
then you have to include the full shared key in the filter.
Syntax:
upsert: <boolean>
We can use upsert option with findAndModify() method. In this method, the default value of this option
is false. If we set the value of this option is true, then the method performs one of the following
operation:
If a document or documents found that matches the given query criteria, then the
findAndModify() method updates the document/documents.
If no document/documents match the given query criteria, then the findAndModify() method
inserts a new document in the collection.
Syntax:
db.Collection_name.findAndModify(
selection_criteria:<document>,
sort: <document>,
remove: <boolean>,
update: <document>,
new: <boolean>,
fields: <document>,
upsert: <boolean>,
bypassDocumentValidation: <boolean>,
writeConcern: <document>,
collation: <document>,
})
For example:
Database: gfg
Collection: employee
Database: three documents that contains details of employees
Now we are going to insert a new document in employee collection by setting the value of upsert option
to true.
db.employee.findAndModify({query:{name:"Ram"},
update:{$set:{department:"Development"}},
upsert:true})
Here, no document matches the name "Ram", so the findAndModify() method inserts a new document
that contains two fields(i.e., name: "Ram" and department: "Development") because the value of the
upsert option is set to true.
Upsert with update() method:
We can use upsert option with update() method. In this method, the default value of this option is false.
If we set the value of this option is true, then the method performs one of the following operations:
If a document or documents found that matches the given query criteria, then the update()
method updates the document/documents.
If no document/documents match the given query criteria, then the update() method inserts a
new document in the collection.
Note: To prevent MongoDB from inserting the same document more than once, create a unique index
on the name field. With a unique index, if multiple documents want the same update with upsert: true,
only one update operation successfully inserts a new document.
Syntax:
db.Collection_name.update({Selection_criteria}, {$set:{Update_data}}, {
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
hint: <document|string>
})
For example:
Database: gfg
Collection: employee
Here, no document matches the name "Priya", so the update() method inserts a new document that
contains two fields(i.e., name: "Priya" and department: "HR") because the value of the upsert option is
set to true.
Upsert with Operator Expressions:
If no document matches the filter from the given collection and the update parameter is a document
that contains update operators, also the value of upsert option is set to true, then the update operation
creates new documents from the equality clauses in the given query parameter and applies the
expressions from the update parameter. Or in other words, when the value of upsert option is true and
no document matches the given filter, then the update operation inserts a new document in the given
collection, and the fields inserted in this new document are the fields that specify in the query and
update documents.
Example:
Collection: example
Now we are going to insert a new document in the example collection by setting the value of the upsert
option to true.
db.example.update({Name: "Rekha"}, // Query parameter
{$set: {Phone: '7841235468 '}, // Update document
$setOnInsert: {Gender: 'Female'}},
{upsert: true})
Here, the update() method creates a new document with field "Name: Rekha" from the query condition
and then applies the $set and $setOnInsert operations to this document.
Note: You are not allowed to specify different _id field values in the query parameter and replacement
document. If you do, then you will get errors.
Example:
Database: gfg
Collection: example
The aggregation pipeline is a multi-stage pipeline, so in each state, the documents taken as input and
produce the resultant set of documents now in the next stage(id available) the resultant documents
taken as input and produce output, this process is going on till the last stage. The pipeline can have 1 to
n number of stagers
If no document matches the given filter and the update parameter contains aggregation pipeline, also
the value of upsert option is set to true then update operation inserts a new document in the collection.
This new document is created from the equality clause present in the query parameter and after that
applies the pipeline to the document to create the document to insert.
Example:
In this example, we are working with
Database: gfg
Collection: employee
Now we are going to insert a new document in the employee collection by setting the value of the upsert
option to true.
Till now, we have studied that the update() method can modify data in the collection based on the query
and if any matching document is not found with help of upsert option new field can be added to the
collection. But upsert with a dotted _id query is an exception and if you attempt to insert a document in
this way MongoDB will show you an error.
Example:
Consider the following update operation. Since the update operation specifies upsert:true and the query
specifies conditions on the _id field using dot notation, then the update will result in an error when
constructing the document to insert.
The remove() method removes documents from the database. It can remove one or all documents from
the collection that matches the given query expression. If you pass an empty document({}) in this
method, then it will remove all documents from the specified collection. It takes four parameters and
returns an object that contains the status of the operation.
This method uses the default write concern because it uses the delete command and the delete
command uses the default write concern. So, if you want to specify a different write concern,
then include the write concern in the optional parameter.
As we know that, this method removes all the documents that match the given matching_criteria,
but we want to remove only one document. So, for this situation set the value of justOne option
to true to remove only one document.
You cannot remove capped collections using the remove() method.
This method can also be used inside a multi-document transaction.
All the remove() method operations perform for the shared collection that specifies the justOne:
true option must contain the shared key/_id field in the query specification. If not then this
operation will return error.
Syntax:
db.Collection_name.remove(
<matching_criteria>,
justOne: <boolean>,
writeConcern: <document>,
collation: <document>
})
Parameter:
This parameter specifies deletion criteria using query operators. To remove all the documents from the
collection, pass an empty document ({}).
Optional Parameters:
justOne: Its default value is false that deletes all documents according to matching criteria. Set
to true if you want to delete only one document.
writeConcern: It is only used when you do not want to use the default write concern. The type
of this parameter is a document.
Collation: It specifies the use of the collation for operations. It allows users to specify the
language-specific rules for string comparison like rules for lettercase and accent marks. The type
of this parameter is a document.
Returns:
This method returns an object that contains the status of the operation.
Examples:
Database: gfg
Collections: student
Document: Three documents contains name and the age of the students
Example 1: Remove all the documents that match the given condition
db.student.remove({name: "Akshay"})
Here, we remove all the documents from the student collection that matches the given condition, i.e,
name: "Akshay".
Here, we remove all the documents from the student collection by passing empty document(i.e., {}) in
the remove() method.
db.student.remove({age:{$eq:18}}, true)
Here, two documents matched the specified condition, but we only want to remove one document, so
we set the value of justOne option to true.
The sort() method specifies the order in which the query returns the matching documents from the
given collection. You must apply this method to the cursor before retrieving any documents from the
database. It takes a document as a parameter that contains a field: value pair that defines the sort order
of the result set. The value is 1 or -1 specify an ascending or descending sort respectively
If a sort returns the same result every time we perform on same data, then such type of sort is
known as a stable sort.
If a sort returns a different result every time we perform on same data, then such type of sort is
known as unstable sort.
MongoDB generally performs a stable sort unless sorting on a field that holds duplicate values.
We can use limit() method with sort() method, it will return first m documents, where m is the
given limit.
MongoDB can find the result of the sort operation using indexes.
If MongoDB does not find sort order using index scanning, then it uses top-k sort algorithm.
Syntax:
db.Collection_Name.sort({filed_name:1 or -1})
Parameter:
The parameter contains a field: value pair that defines the sort order of the result set. The value is 1 or
-1 that specifies an ascending or descending sort respectively. The type of parameter is a document.
Return:
Examples:
Database: gfg
Collections: student
db.student.find().sort({age:1})
db.student.find().sort({age:-1})
db.student.find().sort({name:1})
Return all the documents in the descending order of the age:
db.student.find().sort({name:-1})
In MongoDB, the limit() method limits the number of records or documents that you want. It basically
defines the max limit of records/documents that you want. Or in other words, this method uses on
cursor to specify the maximum number of documents/ records the cursor will return. We can use this
method after the find() method and find() will give you all the records or documents in the collection.
You can also use some conditions inside find to give you the result that you want.
Syntax :
cursor.limit()
Or
db.collectionName.find(<query>).limit(<number>)
Examples:
Database: geeksforgeeks
Collections: gfg
db.gfg.find().limit(2)
Here, we only want the first two documents in the result. So, we pass 2 in the limit method.
Limit only two documents that match the given condition
db.gfg.find({"content":/c/i}).limit(2)
Here, we only want the two documents that satisfy the given condition, i.e., {"content":/c/i}) in the
find() method. Here, content is key were we will check whether it contains 'c' character in the string or
not. /c/ denotes that we are looking for strings that contain this 'c' character and in the end of /c/i, i
denotes that it is case-insensitive.
db.gfg.find({"content":/c/i}).limit(3)
Here, we only want the three documents that satisfy the given condition, i.e., {"content":/c/i}) in the
find() method. Here, content is key were we will check whether it contains 'c' character in the string or
not. /c/ denotes that we are looking for strings that contain this 'c' character and in the end of /c/i, i
denotes that it is case-insensitive.
MongoDB uses various comparison query operators to compare the values of the documents. The following table
contains the comparison query operators:
In the following examples, we are working with:
Database: GeeksforGeeks
Collection: contributor
Document: three documents that contain the details of the contributors in the form of field-
value pairs.
MongoDB supports logical query operators. These operators are used for filtering the data and getting precise
results based on the given conditions. The following table contains the comparison query operators:
In the following examples, we are working with:
Database: GeeksforGeeks
Collection: contributor
Document: three documents that contain the details of the contributors in the form
of field-value pairs.
In MongoDB, we can check the existence of the field in the specified collection using the $exists
operator. When the value of $exists operator is set to true, then this operator matches the document
that contains the specified field(including the documents where the value of that field is null). When the
value of $exists operator is set to false, then this operator returns only those documents that don't
contain the specified field.
Syntax:
{ field: { $exists: <boolean> } }
Examples:
Database: gfg
Collections: student
db.student.find({name:{$exists:true}})
Here, we check the field exists or not in the student collection using $exists operator.
Check the existence of the field of the embedded document:
db.student.find({"details.game":{$exists:true}})
Here, we check the field of the embedded document is exists or not using the $exists operator.
MongoDB provides different types of field update operators to update the values of the fields of the documents
that matches the specified condition. The following table contains the field update operators:
Database: GeeksforGeeks
Collection: Employee
Document: two documents that contain the details of the employees in the form of field-value
pairs.
Updating the value of date field using $currentDate operator:
In the example, we are updating the value of joiningDate field of an employee’s document whose first name is
Om.
Python3
db.Employee.updateOne({"name.first": "Om"},
{$currentDate: {joiningDate: true}})
db.Employee.update({"name.first": "Sumit"},
{$inc: {"personalDetails.salary": 3000}})
Comparing values (or numbers) using $max operator:
In this example, we are comparing values(or numbers) of the salary fields with the specified value, i.e., 40000.
Here, the specified value is greater than the current value. So, $max operator updates the value of the salary field
with the help of update() method to 40000.
Python3
db.Employee.update({"name.first": "Sumit"},
{$max: {"personalDetails.salary": 40000}})
db.Employee.update({"name.first": "Sumit"},
{$min: {"personalDetails.salary": 5000}})
Multiplying the value of a field using $mul operator:
In this example, we are multiplying the value of salary field by 2 in the document who matches the specified
condition, i.e., name = Sumit.
Python3
db.Employee.update({"name.first": "Sumit"},
{$mul: {"personalDetails.salary": 2}})
db.Employee.update({"name.first": "Om"},
{$rename: {"department": "unit"}})
Inserting new fields in new documents using $setOnInsert:
In this example, we are creating new document in Employee collection with the help of update() method by
setting the value of upsert field to true and using $setOneInsert operator assign the values to department and
salary fields in the document.
Python3
MongoDB provides the functionality to search a pattern in a string during a query by writing a regular
expression. A regular expression is a generalized way to match patterns with sequences of characters.
MongoDB uses Perl compatible regular expressions(PCRE) version 8.42 along with UTF-8 support. In
MongoDB, we can do pattern matching in two different ways:
With $regex Operator
Without $regex Operator
This operator provides regular expression capabilities for pattern matching stings in the queries. Or in
other words, this operator is used to search for the given string in the specified collection. It is helpful
when we don't know the exact field value that we are looking in the document. For example, a collection
containing 3 documents i.e.,
{
name: "Tony",
position: "Backend developer"
}
{
name: "Bruce",
position: "frontend developer"
}
{
name: "Nick",
position: "HR Manager"
}
and we are looking for developer information. So, with the help of the $regex operator, we create a
pattern(i.e., {position: {$regex: "developer"}}) that will return only those documents that contain
developer string.
Important Points:
You are not allowed to use `$regex` operator inside `$in` operator.
If you want to add a regular expression inside a comma-separated list of a query condition, then
you have to use the $regex operator.
If you want to use x and s options then you have to use `$regex` operator expression with
`$options`.
Starting from the latest version of MongoDB(i.e., 4.0.7) you are allowed to use `$not` operator
with `$regex` operator expression.
For case-sensitive regular expression queries, if the index of the specified field is available, then
MongoDB matches the regular expression to the values in the index. It is the easiest way to match
rather than scanning all the collections For case-insensitive regular expression queries, they do
not utilize index effectively.
If you want to use Perl compatible regular expressions support regular expressions that are not
supported in JavaScript, then you must use the $regex operator.
Syntax:
$options:
In MongoDB, the following <options> are available for use with regular expression:
i: To match both lower case and upper case pattern in the string.
m: To include ^ and `$ in` the pattern in the match i.e. to specifically search for ^ and $ inside the
string. Without this option, these anchors match at the beginning or end of the string.
x: To ignore all white space characters in the $regex pattern.
s: To allow the dot character "." to match all characters including newline characters.
Examples:
Database: GeeksforGeeks
Collection: employee
Documents: Six documents that contain the details of the employees in the form of field-value
pairs.
Displaying details of employee who are having the word developer in their position field:
Here, we are displaying the documents of those employees whose position field contain developer
string. So we pass a regular expression using $regex operator(i.e. {$regex : "developer"}) for the
position field in the find() method.
Displaying details of employee who are a software engineer with case insensitive by using
i <options>:
db.employee.find({position:{`$regex`:"software",`$options`:"$i"}}).pretty()
Here, we are displaying the documents of those employees whose position field contain case-insensitive
"software" string. So, we pass a regular expression with option(i.e., {`$regex` : "software", `$options`:
"$i"}) for the position field in the find() method.
In the regular expression, `$options`:"$i" is used to match both lower case and upper case pattern in the
given string(i.e., "software").
db.employee.find({Name:{$regex:"^B"}}).pretty()
Here, we are displaying the documents of those employees whose name starts with 'B' letter. So, we
pass a regular expression using $regex operator(i.e. {$regex : "^B"}) for the Name field in the find()
method.
db.employee.find({Name:{`$regex`:"e$"}}).pretty()
Here, we are displaying the documents of those employees whose names end with the 'e' letter. So, we
pass a regular expression using $regex operator(i.e. {`$regex` : "e$"}) for the Name field in the find()
method.
In MongoDB, we can do pattern matching without using the `$regex` operator. Simply, by using a regular
expression object to specify a regular expression. Also, by using a regular expression object you are
allowed to use a regular expression inside $ in operator.
Syntax:
{ <field>: /pattern/<options> }
Example:
Database: GeeksforGeeks
Collection: employee
Documents: Six documents that contain the details of the employees in the form of field-value
pairs.
Displaying details of employee who are having the word "te" in their name by using
regular expression object:
db.employee.find({Name: /te/}).pretty()
Here, we are displaying the documents of those employees whose names contain the "te" string. So, we
pass a regular expression(i.e., {Name: /te/}) for the Name field in the find() method. In this regular
expression, // means to specify your search criteria in between these delimiters, i.e., /te/.
Aggregation in MongoDB - Fork MongoDB
In MongoDB, aggregation operations process the data records/documents and return computed
results. It collects values from various documents and groups them together and then performs
different types of operations on that grouped data like sum, average, minimum, maximum, etc to return
a computed result. It is similar to the aggregate function of SQL.
Aggregation pipeline
Map-reduce function
Single-purpose aggregation
Aggregation pipeline
In MongoDB, the aggregation pipeline consists of stages and each stage transforms the document. Or in
other words, the aggregation pipeline is a multi-stage pipeline, so in each state, the documents taken as
input and produce the resultant set of documents now in the next stage(id available) the resultant
documents taken as input and produce output, this process is going on till the last stage. The basic
pipeline stages provide filters that will perform like queries and the document transformation modifies
the resultant document and the other pipeline provides tools for grouping and sorting documents. You
can also use the aggregation pipeline in sharded collection.
Let us discuss the aggregation pipeline with the help of an example:
In the above example of a collection of train fares in the first stage. Here, the ]match stage filters the
documents by the value in class field i.e. class: "first-class" and passes the document to the second stage.
In the Second Stage, the group stage groups the documents by the id field to calculate the sum of fare
for each unique id.
Here, the aggregate() function is used to perform aggregation it can have three operators stages,
expression and accumulator.
$match: It is used for filtering the documents can reduce the amount of documents that are
given as input to the next stage.
$project: It is used to select some specific fields from a collection.
$group: It is used to group documents based on some value.
$sort: It is used to sort the document that is rearranging them
$skip: It is used to skip n number of documents and passes the remaining documents
$limit: It is used to pass first n number of documents thus limiting them.
$unwind: It is used to unwind documents that are using arrays i.e. it deconstructs an array field
in the documents to return documents for each element.
$out: It is used to write resulting documents to a new collection
Expressions: It refers to the name of the field in input documents for e.g. { $group : { _id : "$id",
total:{$sum:"$fare"}}} here $id and $fare are expressions.
Note:
Examples:
Database: GeeksForGeeks
Collection: students
Documents: Seven documents that contain the details of the students in the form of field-value
pairs.
Displaying the total number of students in one section only
Displaying the total number of students in both the sections and maximum age from both
section
In this example, we use $group to group, so that we can count for every other section in the documents,
here $sum sums up the document in each group and $max accumulator is applied on age expression
which will find the maximum age in each document.
Displaying details of students whose age is greater than 30 using match stage
db.students.aggregate([{$match:{age:{$gt:30}}}])
In this example, we display students whose age is greater than 30. So we use the $match operator to
filter out the documents.
Sorting the students on the basis of age
In this example, we are using the $sort operator to sort in ascending order we provide 'age':1 if we want
to sort in descending order we can simply change 1 to -1 i.e. 'age':-1.
Unwinding works on array here in our collection we have array of subjects (which consists of different
subjects inside it like math, physics, English, etc) so unwinding will be done on that i.e. the array will be
deconstructed and the output will have only one subject not an array of subjects which were there
earlier.
db.students.aggregate([{`$unwind`:"$subject"}])
Map Reduce
Map reduce is used for aggregating results for the large volume of data. Map reduce
has two main functions one is a map that groups all the documents and the second one
is the reduce which performs operation on the grouped data.
Syntax:
Example:
Database: GeeksForGeeks
Collection: studentsMark
Documents: Seven documents that contain the details of the students in the form of field-
value pairs.
var mapfunction = function(){emit(this.age, this.makrs)}
Now, we will group the documents on the basis of age and find total marks in each age
group. So, we will create two variables first mapfunction which will emit age as a key
(expressed as "_id" in the output) and marks as value this emitted data is passed to our
reducefunction, which takes key and value as grouped data, and then it performs
operations over it. After performing reduction the results are stored in a collection here
in this case the collection is Results.
It is used when we need simple access to document like counting the number of documents or for
finding all distinct values in a document. It simply provides the access to the common aggregation
process using the count(), distinct(), and estimatedDocumentCount() methods, so due to which it lacks
the flexibility and capabilities of the pipeline.
Example:
Database: GeeksForGeeks
Collection: studentsMark
Documents: Seven documents that contain the details of the students in the form of field-value
pairs.
Here, we use a distinct() method that finds distinct values of the specified field(i.e., name).
db.studentsMarks.count()
Here, we use count() to find the total number of the document, unlike find() method it does not find all
the document rather it counts them and return a number.