Mongodb Cheat Sheet: Click Here
Mongodb Cheat Sheet: Click Here
© Copyright by Interviewbit
Contents
Page © Copyright by
MongoDB Tutorial: Beginners and
Experienced (....Continued)
Page © Copyright by
Let's get Started
Introduction
MongoDB is a data management platform that enables quick and easy query
development and deployment of online, real-time data applications. It is a
distributed, not-backed store that runs on a collection of servers and uses a JSON-
like data model.
MongoDB Replica Management allows you to easily and cost-effectively scale your
MongoDB architecture. MongoDB provides a rich set of analytical tools for data
profiling, load analysis, and monitoring. It can be used for a variety of purposes
including data mining, Big Data, and online analytical processing.
Internet and enterprise application developers that require flexibility and scaling
efficiently may consider using MongoDB. MongoDB is particularly suited to
developers of varied types who are creating scalable applications using agile
approaches.
MongoDB has both pros and cons just like other NoSQL databases.
Pros:
Page © Copyright by
MongoDB Cheat
Any type of data can be stored in MongoDB, which gives users the flexibility to
create as many fields in a document as they desire.
Documents map to native data types in many programming languages, which
provides a means of adding to data. Sharding, which involves dividing data
across a cluster of machines, is also achieved by this.
MongoDB includes its own file system, similar to the Hadoop Distributed File
System (HDFS), called GridFS. The file system is primarily used to store files that
exceed MongoDB's 16 MB per document BSON size limit.
MongoDB is also compatible with Spark, Hadoop, and other data processing
frameworks like SQL.
Cons:
When a MongoDB master node goes down, another node will automatically
become the new master. Despite the fact that it promises continuity, the automatic
failover strategy is not instantaneous - it may take up to a minute. In contrast, the
Cassandra NoSQL database supports multiple master nodes, so that if a master
goes down, another one is ready to run a highly available database infrastructure.
Although MongoDB's single master node restricts how fast data can be written
to the database, it also limits how much data can be written. Because of this,
data writes must be recorded on the master, and new information cannot be
added to the database quickly.
MongoDB doesn't provide full referential integrity using foreign-key constraints,
which could affect data consistency.
User authentication isn't enabled by default in MongoDB databases. Because of
this, there is a default setting that blocks networked connections to databases if
they've not been configured by a database administrator.
There have also been instances of ransomware attacks that forced the setting to be
turned on by the database administrator.
Features of MongoDB
Page © Copyright by
MongoDB Cheat
1. Replication: The MongoDB replica set feature is known for providing high
availability. Two or more copies of data constitute a replica set. A replica-set acts as
a primary or a secondary replica. Secondary replicas keep a copy of the data of the
primary, preserving it in an orderly manner, as part of a replicated MongoDB
system. Whenever a primary replica crashes, the replica set automatically
determines which secondary should become the primary and conducts an election if
necessary. Secondary replicas may additionally serve read operations, but the data is
only eventually consistent by default. To resolve the election of the new primary,
three standalone servers must be added as secondary servers.
2. Indexing: A MongoDB field can be indexed with primary and secondary indices
or indexes. A MongoDB index stores a small portion of the data set in a form that is
convenient to traverse. The index stores the value of a particular field, or set of
fields, ordered by their value. In MongoDB, indexes assist in efficiently resolving
queries by storing a small portion of the data set in a convenient form. A MongoDB
index is similar to a typical relational database index.
3. File storage: GridFS, which uses MongoDB as a file system, can be used to
balance and replicate data across multiple machines. A file can be stored in
MongoDB as a grid file system. It has features similar to a file system such as load
balancing and data replication.
4. Aggregation: The aggregation pipeline, the map-reduce function, and single-
purpose aggregation methods are available in MongoDB. According to MongoDB's
documentation, the Aggregation Pipeline provides better performance for most
aggregation operations over map-reduce. With the aggregation framework, users
can obtain the kind of results for which the SQL GROUP BY clause is used. The
aggregation framework includes $lookup and standard deviation like statistical
operators.
5. Sharding: Sharding is the splitting up of data among machines. To permit this,
we refer to it as "partitioning" or "sharding." We may store more data and handle
more load without upgrading our machines, by dividing data across them.
MongoDB's sharding allows you to split up a collection among many machines
(shards), allowing it to grow beyond resource limitations.
Page © Copyright by
MongoDB Cheat
The following cheat sheet is filled with some handy tips and commands for quick
reference:
4. Drop a database
db.dropDatabase()
5. Inserting Document
The MongoDB shell provides the following methods to insert documents into a
collection:
db.docx.insert({name:'Enterprise',operator:'Star',type:'Explorer',class:'Universe',cre
6. Insert Row
w db.docx.insert({name:'Prometheus',operator:'Star',class:'Prometheus',crew:40,codes:
Page © Copyright by
MongoDB Cheat
8. Finding document
The MongoDB shell provides the following methods to find documents:
Page © Copyright by
MongoDB Cheat
Finds one
1. db.docx.findOne() random
document.
2. db.docx.find().prettyPrint()
Finds all
documents.
Displays only
the names of
db.docx.find({}, {name:true,
3. the document
_id:false})
Docx.
Page © Copyright by
MongoDB Cheat
db.docx.find({class:
$gt greater than
{$gt:'T'}
db.docx.find({class:
$lt lesser than
{$lt:'T'}
does an db.docx.find({class:
$exists attribute exist
{$gt:'T'}
or not
Matching db.docx.find({name:
$regex pattern in pearl-
{$regex:'^USS\\sE'}})
style
Page © Copyright by
MongoDB Cheat
deleteOne and deleteMany can be used for this purpose. Both of these methods take a
filter document as their first parameter.
db.docx.deleteOne({"_id" : 6})
# asc
#db.docx.find().sort({
desc title: 5 }).pretty()
14.db.docx.find().sort({
Count Rows title: -5}).pretty()
Page © Copyright by
MongoDB Cheat
20. Aggregation
1.
Page © Copyright by
MongoDB Cheat
db.docx.aggregate([{$group
Sum up : {_id : "$operator",
$sum
values num_docx : {$sum :
"$value"}}}])
db.docx.aggregate([{$group
Calculates
: {_id : "$operator",
$avg average
num_docx : {$avg :
values
"$value"}}}])
db.docx.aggregate([{$group
Find
$min / : {_id : "$operator",
min/max
$max values num_docx : {$min :
"$value"}}}])
db.docx.aggregate([{$group
Push values
: {_id : "$operator",
$push to a result
classes : {$push:
array
"$value"}}}])
db.docx.aggregate([{$group
To get the
$first / : {_id :
first / last
$last document "$operator",
last_class : {$last
: "$value"}}}])
Page © Copyright by
MongoDB Cheat
Page © Copyright by
MongoDB Cheat
use test
db.getSiblingDB("dbname")
Change Streams
db.currentOp()
db.killOp(345) // opid
Page © Copyright by
MongoDB Cheat
Conclusion
MongoDB is one of the world’s most popular document databases. It has some
powerful capabilities like full-text search, data aggregation etc. One should have a solid
knowledge of what MongoDB is. In this document, we’ve covered the basics of
MongoDB, its features, and some of the important cheat sheets. We’ve also explored
the common database operations of MongoDB. Now, it’s time for you to head out and
try what we’ve covered here and more.
Useful Resources
Page © Copyright by
Links to More Interview Questio
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions
Page © Copyright by