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

Create operation: This operation is used to insert new documents into the database.

Inserts are the basic method


for adding data to MongoDB. If the specified collection (table) does not exist, the insert operation will create the
collection automatically during its execution. There are two create operations that can be used to insert documents
into a collection: insertOne, insertMany. While methods such as insert are still supported for backward compatibility,
they should be avoided. You should prefer using insertone and insertMany for creating documents. insertone allows
to insert one document in a collection. To insert multiple documents at one time, use insertMany method.

Read operation: This operation is used to query documents in the database. There are two methods for querying
documents from a collection in MongoDB: find, findOne. The find (or findore) method is used to perform queries in
MongoDB. It returns a subset of documents. It means that no documents or all documents from the collection can
be returned as a result of query. The first argument specifies which documents get returned. The second argument
specifies the fields (keys) you want to display in the result. The only difference between find and findOne is that, if
multiple documents satisfy the query, the findOne method returns the first document according to the natural
order. The find method returns all documents from a collection based on query criteria.

Update operation: This operation is used to update existing documents in the database. There are different methods
of updating documents: updateOne, updateMany, replaceOne, findOneAndUpdate, findOneAndReplace. updateOne
and updateMany each take a filter document as their first parameter and a modifier document as a second
parameter describing changes to make. To update a document, updateOne method with two parameters namely, an
update filter and an update action is used. The updateMany is used to update multiple items by passing in a list of
items. This uses the same syntax for updating a single document. If two updates occur at the same time, then
whichever reaches the server first will be applied and then the next one will be considered.

Delete operation: This operation is used to remove documents from a collection. There are three methods for
deleting documents from a collection: deleteOne, deleteMany, findOneAndDelete. deleteOne - removes a single
document from a collection on the basis of a condition mentioned as a parameter. deleteMany - removes multiple
documents from a collection in single step. To delete all the documents that matches a given condition
(filter), use deleteMany.

Create Commands - The first operation of CRUD used to create new documents in a collection. MongoDB offers
three methods to create new documents: insert, insertOne, insertMany. The createCollection command is used to
create an empty collection(having no documents) explicitly. On the other hand, the db.collection_name.insertOne or
db.collection_name.insertMany creates the collection implicitly. This means that when you give insertOne or
insertMany command the collection name mentioned in the command is automatically created and documents are
inserted into it in MongoDB.

createCollection - This command creates a new collection or view. Syntax: db.createCollection (collection_name,
options) Here, the collection_name is the name who wants to give to newly created collection. The options are:
capped, autoIndexId, size, max, storageEngine, validator, validationLevel, validationAction, indexOptionDefaults,
viewon, pipeline, collation, writeConcern. If capped option is true(capped collection), then you have to set the
maximum size in the size option. The autoIndexId option, if set to false, disables the automatic creation of index on
the "_id" field. The size option must be specified if capped option is set to true. It specifies maximum size in bytes for
a capped collection. The max option is used to specify the maximum number of documents. Ensure that the value for
the size option is sufficient enough to contain the value specified in the max option. The storageEngine option is
available only for the WiredTiger storage engine. The validator option is used to specify validation rules or
expressions for the collection. The validationLevel option specifies different levels of validation like strict, moderate,
off. The validationAction option decides to display a warning about invalid documents, but allows them to be
inserted or display an error message. The indexOptionDefaults option specifies a default configuration for indexes
when a collection is being created. The viewon option specifies the source collection or view name from which the
view is created.

insert - insert was the primary method for inserting documents into MongoDB. It is suggested to use insertOne and
insertMany method instead of insert for creating documents. Syntax: db.collection_name.insert(document, options)
The first parameter is a document to be inserted into the collection. The options are: writeConcern, ordered. Both
options are not compulsory. You can omit the writeConcern option, to use the default write concern as w:1. Do not
explicitly specify the writeConcern for an individual operation if run in a transaction. For writeConcern with
transactions. If the ordered option is true, MongoDB performs an ordered insert of the documents in the array. In
case of an error, MongoDB returns the remaining documents in the array without processing. The
default value is true.

insertOne - It adds a document into a collection. Syntax: db.collection_name.insertOne (document, option) The first
parameter is a document to insert into the collection. The second parameter is writeConcern.

insertMany - It adds multiple documents in a collection. With insertMany command you can insert an array of
documents. Syntax: db.collection_name([document1, document2, ], options) The options are: writeConcern,
ordered. Both the options are not compulsory. The ordered option accepts boolean value which specifies whether
MongoDB should perform an ordered or unordered insert. The default value is true. Read commands The second
operation of CRUD used to read/fetch the documents from a collection. MongoDB offers two methods to fetch the
data from a collection: findOne, and find.

findOne: findOne returns the first document that satisfies the query according to the natural order. Syntax:
db.collection_name.findOne (query, projection) In the syntax, collection_name is the name of the collection from
which the document is to be retrieved. The first parameter is query which is optional. It specifies the query selection
criteria using query operators. The second parameter is projection which is also optional. It specifies the fields to
return using projection operators. If you want to return all fields from a collection, drop second parameter. If you
specify projection parameter, findOne returns a document that only contains the projection fields. By default, "_id"
field is always included in the projection fields. The projection parameter takes the document of the following form:
{ <field_1>: <value>, <field_2>: <value> .. } For example, {<field>: <1 or true>} shows that the field is included.
{<field> : <0 or false>} shows that the field is excluded from the result.

find: $nor $exis $type Exam No 1.find selects documents from a collection or view and returns a cursor to the
selected document. Syntax: db.collection_name.find(query, projection) In the syntax, collection_name is the name of
the collection from which a cursor to the selected document is returned. The first parameter is query which is
optional. It specifies the query selection criteria using query operators. The second parameter is projection which is
also optional. It specifies the fields to return using projection operators. If you want to return all fields from a
collection, drop second parameter. If you specify projection parameter, find returns a cursor to the documents that
only contain the projection fields. By default, "_id" field is always included in the projection fields. The projection
parameter takes the document of the following form: { <field_1>: <value>, <field_2>: <value> _. } For example,
{<field>: <1 or true>} shows that the field is included. {<field>:<0 or false>} shows that the field is excluded
from the result.

Update Commands - You can update specific fields in a document using atomic update operators. These are used to
specify complex update operations like modifying, adding or removing keys and even manipulating arrays and
embedded documents.

update : The update operation is required when you want to modify the existing document/ documents in a
collection. Generally, the update operation is carried out based on some condition. This method updates a single
document. By including the option "multi:true" all the documents that match the query criteria can be updated.
Syntax: db.collection_name.update(condition, update, options) The collection_name specifies the name of the
collection whose documents are being updated. The first parameter condition is the criteria for update. The second
parameter update means the modifications to apply. The third parameter options is optional which can be any of the
following: upsert, multi, writeConcern, collation, arrayFilters, hint. upsert takes boolean values. The default value for
upsert is false. When upsert is true, either of the two actions taken: (i) MongoDB creates a new document if no
documents satisfying the query condition are found. (ii) MongoDB updates a single document that matches the
query condition.

updateOne - This updates a single document within a given collection based on the condition. It updates the first
matching document in the collection that satisfies the condition. Syntax: db.collection_name.updateOne (condition,
update, options) The collection_name specifies the name of the collection whose documents are being updated. The
first parameter condition is the criteria for update. The second parameter update means the modifications to apply.
The third parameter options is optional which can be any of the following: upsert, writeConcern, collation,
arrayFilters, hint. The upsert, writeConcern, collation, arrayFilters, hint options for updateOne are similar to the
options used in update method.

updateMany - This updates all documents that match the specified condition for a collection. Syntax:
db.collection_name.updateMany (condition, update, options) The collection_name specifies the name of the
collection whose documents are being updated. The first parameter condition is the criteria for update. The second
parameter update means the modifications to apply. The third parameter options is optional which can be any of the
following: upsert, writeConcern, collation, arrayFilters, hint. The upsert, writeConcern, collation, arrayFilters, hint
options for updateMany are similar to the options used in update method.

replaceOne : This method replaces a matching document with the new one. This can be helpful for schema
migration. It replaces the first matching document in the collection that satisfies the condition. Syntax:
db.collection_name.replaceOne (condition, replacement, options) The first parameter condition is the selection
criteria for update. The second parameter replacement is the replacement document. It cannot contain update
operators. Options can be any of these: upsert, writeConcern, collation, hint. The upsert option is optional. When set
to true, replaceone either inserts the document from the replacement parameter if no document matching the
condition is found or replaces the document that matches the condition with the replacement document. If"_id"
field is not specified in condition or replacement document, MongoDB will add the "_id" field to the replacement
document. If "_id" is present in condition and replacement document, the values must be identical. The default
value of upsert is false. writeConcern, collation, hint options are similar to the options used in update

findOneAndUpdate - It updates a single document based on the condition and sort criteria. The important difference
between findOneAndUpdate and updateOne is that findOneAndUpdate returns a document but updateOne does
not return a document. Syntax: db.collection_name.findOneAndUpdate (condition, update, options) The first
parameter condition is the selection criteria for update. The second parameter update is the update document or
aggregation pipeline. It contains only update operator expressions and cannot specify replacement document. The
options are: projection, sort, maxTimeMS, upsert, returnNewDocument, collation, arrayFilters. All the options are
not compulsory. The projection option if specified, returns a subset of fields. The sort option specifies a sorting order
for the documents which satisfy the condition. The maxTimeMS specifies a timeout in milliseconds. After the timeout
is reached, an error is displayed. The upsert option if set to true, either creates a new document (if no documents
satisfy the condition mentioned as the first parameter) or updates a single document that satisfies the condition. The
default value is false.

findOneAndReplace - It replaces a single document based on the specified condition. Syntax:


db.colletion_name.findOneAndReplace (condition, replacement, options) The first parameter condition is the
selection criteria for update. The second parameter replacement is the replacement document. It cannot contain
update operators. The options are: projection, sort, maxTimeMS, upsert, returnNewDocument, collation. All the
options are not compulsory. The projection option if specified, returns a subset of fields. The sort option mentions a
sorting order for the documents which satisfy the condition. The maxTimeMS mentions timeout in milliseconds.
After the timeout is reached, an error is displayed. The upsert option if set to true, either creates a new document (if
no documents satisfy the condition mentioned as the first parameter) or replaces a single document that satisfies
the condition. If"_id" is present in condition and replacement document, the values must be identical. The default
value of upsert is false. The returnNewDocument option if set to true returns the replacement document (instead of
old document). The default value is false. The collation option allows to mention language-specific rules for
string comparison.

Delete Commands - The delete operations remove the documents from the collection. MongoDB provides the
following methods to delete the documents of the collection. 1. db.collection.deleteOne() 2.
db.collection.deleteMany() 3. drop()

deleteOne - The delete0ne takes condition as the first parameter. The condition specifies a set of criteria to match
against in removing documents. Syntax: db.collection_name.deleteOne (condition, options) The first parameter
mentions the condition as deletion criteria. The options are: writeConcern, collation, hint. All the options are not
compulsory. The collation option allows to mention the language-specific rules for string comparison. The hint
option is a document that specifies the index to use to assist the query predicate.
deleteMany - To delete all documents that match a condition, use deleteMany. Syntax:
db.collection_name.deleteMany (condition, options) The first parameter mentions the condition as deletion criteria.
The options are: writeConcern, collation. The collation option allows to mention language-specific rules for
string comparison. drop Removing documents using deleteMany is a quick operation. But if you want to remove an
entire collection, it is faster to drop it. Syntax: db.collection_name.drop()

findOneAndDelete - findOneAndDelete deletes the first matching document that satisfies the condition mentioned.
Further, the "sort" option is used to decide which document is deleted. findOneAndDelete returns the deleted
document. Syntax: db.collection_name.findOneAndDelete(condition, options) The first parameter mentions the
condition as deletion criteria. The options are: projection, sort, maxTimeMS, collation. All the options are not
compulsory. The projection option specifies which fields to return in the returned document. You can skip this
option, if you want to return all fields in the returned document. The sort option is related with the sorting order of
the documents. The maxTimeMS specifies the deadline (in milliseconds) within which the operation should
complete. The collation option allows to mention language-specific rules for string comparison. remove The remove
method removes the documents that satisfy a condition. Syntax: db.collection_name.remove(condition, options) The
first parameter condition is compulsory while the options is not compulsory.

Index Concepts sus Single-key indexes: The default index on "_id" is the best example of a single-key index. With a
single-key index, every entry in the index corresponds to a single value from each of the documents indexed. 2.
Compound key indexes: You often need to query on more than one attribute. You want such query to be as efficient
as possible. The order of keys in a compound index matters. Generally a query where one term demands an exact
match and another term specifies a range requires a compound index in which range key comes second. MongoDB
represents most indexes internally as B-trees. B-trees are ideal for database indexes. B-trees facilitate a variety of
queries, like exact matches, range conditions, sorting, prefix matching, and index-only queries. B-trees are able to
remain balanced inspite of the addition and removal of the keys. In MongoDB's B-tree implementation, a new node
is allotted 8192 bytes. This means that each node may contain hundreds of keys. This depends on the average index
key size, We know that each node is 8 KB and it can be estimated how many keys will fit into each node. B-tree
nodes are usually kept around 60% full by default. information is useful to decide when to index on your collections
and when to avoid the indexes.

Index Properties : Unique Indexes: Unique indexes impose uniqueness across all their entries. To create a unique
index, specify the unique option. Syntax: db.collection_name.createIndex({field_name: 1}, {unique: true}) If you
require a unique index on specific collection, it is best to create the index before inserting any data. This approach
guarantees the uniqueness condition from the start. Creating a unique index on a collection already having some
data can be problematic. This is because it is possible that duplicate keys may be there in the collection. In case of
duplicate keys, the index creation fails.

Example: In this example, we have a "product" collection having "_id" and "name" fields. > db.product.find() {"_id" :
100, "name": "fountain pen" } {"_id" : 101, "name": "ballpoint pen" }

We insert another document having {"name":"fountain pen"}. > db.product.insertOne({"_id":102, "name":


"fountain pen"}) { "acknowledged": true, "insertedId": 182 }

Index Creation - Index is created by using createIndex method. It has following syntax: Syntax:
db.collection_name.createIndex({keys}, {options}) Keys as specified in ("field_name":1} for ascending index or
("field_name": -1} for descending index. Keys can be single field, compound key, multi key. The options are: unique,
sparse, partialFilterExpression etc.

INTRODUCTION TO AGGREGATION - The aggregation framework is MongoDB's advanced query language which
allows you to transform and combine data from multiple documents which in turn generates new information not
available in any individual document. The aggregation framework can be used to calculate sales by month, sales by
region etc. The people who have familiarity with RDBMS can think about the MongoDB's aggregation framework as
an equivalent to the GROUP BY clause of SQL.
APPROACH TO AGGREGATION - A call to the aggregation framework defines a pipeline. In the aggregation pipeline,
the output from each step is provided as an input to the next immediate step. Each step carries out single operation
on the input documents. Aggregation pipeline operations are explained as follows: $project: Specify fields to be
placed in the output document. $match: Select documents to be processed. $group: Group documents by a user
specified key. $skip: Skip a user specified number of documents. $limit: Limits the number of documents to be
passed to the next step. $unwind: Expand an array, generating one output document for each array entry. $out:
Write the results of the pipeline to a collection. $sort: Sort documents. $geoNear: Select documents near a
geospatial location. $redact: Control access to certain data.

PERFORMANCE TUNING : Some important points to consider regarding the performance of aggregation pipeline:
Indexes can only be used by $sort and $match operations. It can improve the speed of these operations. Try to
reduce the number and size of documents earliest in your pipeline. You could not use an index after your pipeline
uses an operator other than $sort and $match. If sharding is used, the $project and $match operators will be run on
individual shards. Once you use any other operator, the remaining pipeline will be run on the primary shard. The
MongoDB explain function describes query paths and allows developers to diagnose slow operations by determining
indexes that a query has used. The explain function for the aggregation framework is a bit different from the explain
function used in find method. But it provides similar capabilities. For an aggregation pipeline, for each individual
operation in the pipeline output is generated by the explain function.

ADMINISTRATION CONCEPTS IN MongoDE - The administration mentions strategies and practices to be used in the
operation of MongoDB systems. These are as following: MongoDB backup methods: It describes the techniques for
backing up a MongoDB database. Operational policies: Key concepts for the operation and maintenance of
MongoDB deployments are documented. Management of data: Addresses issues in data management,
maintenance, organization. Capped collections: It provides predefined size collections that preserve insertion order
and can assist high volume inserts.

Monitoring: It includes an overview of monitoring tools, approaches to monitoring replica sets and sharded clusters,
diagnostic tools. Run time database configuration. Awareness about data center: It presents features that allow
database administrators and application developers to model their deployments to be aware about data center.
Expiring data from collections: By setting TTL, it is possible to automatically delete data from a collection based on
the value of a timestamp. This can be useful in managing data that are only required for a limited timespan.

MONITORING ISSUES RELATED TO DATABASE Any cloud-based technology requires monitoring its performance to
design a successful product. The same is applied for MongoDB. Performance degradation may cause poor user
experience and downtimes. Because of the monitoring, the problems can be detected before they can lead to
failures. By monitoring MongoDB you can keep watch on utilization of resources. You can also observe the issues
related to performance.

MONITORING AT SERVER, DATABASE, COLLECTION LEVEL, AND VARIOUS MONITORING TOOLS RELATED TO
MongoDB : We can monitor MongoDB databases by using different tools like mongostat, mongotop, dbStats,
collStats, and serverStatus commands. These commands provide real-time monitoring and reporting of the database
server. It allows us to monitor errors and database performance and assist in informed decision making to
optimize a database.

Database Profiling - The database profiler gathers information about database commands executed on a running
mongod instance. The profiler writes all data into a profile collection. By default, the profiler is off. The profiling level
'0' means the profiler is off and does not gather any data. the profiling level '1' means the profiler gathers data for
operations that take longer than the value of second argument(i.e. slowms). The profiling level '2' means that the
profiler gathers data for all operations.

Locks - MongoDB allows concurrency. To preserve the consistency, it uses locking inechanism. MongoDB utilizes
multi granularity locking which allows operations to lock at the highest (global level) to lowest level (document level
in WiredTiger). We can get the details about the locks by using db.serverStatus() or db.current0p() commands. The
lock modes mentioned are as follows: The "R" mode is related with Shared lock(S). The "W" mode is related with
Exclusive lock(X). The "r" mode is related with Intent Shared lock(IS). The "w" mode is related with Intent Exclusive
lock(IX). > db.runCommand({"serverStatus": 1, "locks":1})
Memory Usage - To display the memory usage statistics in MongoDB, use the following command:

> db.serverStatus().mem { "bits": 64, "resident": 30, "virtual": 5532, "supported": true } The "bits" field shows the
MongoDB is installed on 64-bit system. The "resident" field displays the amount of resident memory in use. The
"virtual" field shows the quantity of virtual memory used by MongoDB process. The "supported" field is set to true
means that the system supports extended memory information. MongoDB in its default configuration will use max
(256MB, 1*(RAM Size-1GB)} for its cache size. In MongoDB, WiredTiger uses prefix and snappy block compression for
indexes and collections respectively. The WiredTiger uses both the filesystem and WiredTiger internal cache. The
filesystem cache is utilized by the operating system to decrease disk input/output. Through the filesystem cache,
MongoDB automatically utilizes free memory which is unused by the WiredTiger cache or other processes.

Page Fault - In operating system, page fault is a situation when the physical memory is completely used up and pages
of physical memory are swapped to disk. You can observe page fault statistics per database using following
command: > db.adminCommand({"serverStatus": 1})["extra_info"]

{"note": "fields vary by platform", "page_faults": 369157, "usagePageFileMB": 220, "totalPageFileMB": 14256,
"availPageFileMB": 5435, "ramMB": 8112  }

BACKUP AND RECOVERY METHODS FOR MongoDB There are a number of choices for backing up clusters in
MongoDB. The MongoDB Atlas (cloud service) provides not only continuous backups but also cloud provider
snapshots. Continuous backups take incremental backups of data in your cluster. Cloud provider snapshots provide
localized backup storage using the snapshot feature of the cloud service provider. There are various cloud service
providers like Amazon Web Services, Microsoft Azure, Google Cloud Platform. The best choice for backup is
continuous backups. MongoDB also provides backup capability with the help of Cloud Manager and Ops Manager.
Cloud Manager is hosted backup, monitoring and automation service for MongoDB. Ops Manager is an on-premise
solution that has similar functionality to Cloud Manager. When failure occurs, a backup strategy can save our data.
Hence backups play an important role.

EXPORT AND IMPORT OF DATA TO AND FROM MongoDB The import method is needed to migrate an existing
system to MongoDB to seed the database with information from a data warehouse. A good export strategy may also
be required because you may need to export data from MongoDB to external processing jobs.There are two ways to
import and export data with MongoDB: Write a simple program using one of the drivers. Use the included tools like
mongoimport and mongoexport. When you have the need to export a rich document to CSV or import a CSV to a
rich MongoDB document, it may be easier to build a custom tool. You can build it using any of the drivers. For
example, MongoDB users generally design scripts that connect to a relational database and then combine the data
from two tables into a collection. The difficult part about movement of data in and out of MongoDB: the way the
data is modelled may differ between systems.

mongoimport Command - You can use mongoimport to import JSON, CSV and TSV files. This is required for loading
data from relational databases into MongoDB. The mongoimport command is executed from the command line and
not from the mongo shell. Syntax: mongoimport options file_name Some of the important options are --db, --
collection etc.

mongoexport command - can use mongoexport to export all of a collection's data to a JSON or CSV file. Syntax:
mongoexport options out_file Some of the important options are --db,--host, --collection, --readPreference etc. The
above command exports data to a file info.csv. You can refer the mongoexport's command options by starting it with
the --help option.

You might also like