0% found this document useful (0 votes)
55 views70 pages

Questions

Uploaded by

kamleshrani2410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views70 pages

Questions

Uploaded by

kamleshrani2410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Questions

28 July 2024 12:30 PM

MongoDB
Interview
Questions

Introduction to MongoDB
When dealing with data,
there are two types of data
as we know – (i) structured
data and (ii) unstructured
data. Structured data is usually
stored in a tabular form
whereas unstructured data is
not. To manage huge sets of
unstructured data like log or
IoT data, a NoSQL database is
used.
What is MongoDB ?
• MongoDB is an open-
source NoSQL
database written in C++
language. It uses
JSON-like documents
with optional schemas.
• It provides easy
scalability and is a
cross-platform,
document-oriented
database.
• MongoDB works on the
concept of Collection
and Document.
• It combines the ability
to scale out with
features such as
secondary indexes,
range queries, sorting,
aggregations, and
geospatial indexes.
• MongoDB is developed
by MongoDB Inc. and
licensed under the
Server Side Public
License (SSPL).
MongoDB Basic
Interview
Questions
1. What are some of
the advantages of
MongoDB?
Some advantages of
MongoDB are as follows:
• MongoDB supports
field, range-based,
string pattern matching
type queries. for
searching the data in

Mongoose Page 1
searching the data in
the database
• MongoDB support
primary and secondary
index on any fields
• MongoDB basically
uses JavaScript objects
in place of procedures
• MongoDB uses a
dynamic database
schema
• MongoDB is very easy
to scale up or down
• MongoDB has inbuilt
support for data
partitioning (Sharding).
Create A FREE Custom
Study Plan
Get into your dream
companies with expert..
Real-Life Problems
Prep for Target Roles
Custom Plan Duration
Flexible Plans
Create My Plan
2. When to use
MongoDB?
You should use MongoDB
when you are building
internet and business
applications that need to
evolve quickly and scale
elegantly. MongoDB is
popular with developers of
all kinds who are building
scalable applications using
agile methodologies.
MongoDB is a great choice if
one needs to:
• Support a rapid iterative
development.
• Scale to high levels of
read and write traffic -
MongoDB supports
horizontal scaling
through Sharding,
distributing data across
several machines, and
facilitating high
throughput operations
with large sets of data.
• Scale your data
repository to a massive
size.
• Evolve the type of
deployment as the
business changes.
• Store, manage and
search data with text,
geospatial, or time-
series dimensions.
3. What are the data
types in MongoDB?
MongoDB supports a wide
range of data types as
values in documents.

Mongoose Page 2
values in documents.
Documents in MongoDB are
similar to objects in
JavaScript. Along with
JSON’s essential key/value–
pair nature, MongoDB adds
support for a number of
additional data types. The
common data types in
MongoDB are:
• Null
{"x" : null}
• Boolean
{"x" : true}
• Number
{"x" : 4}
• String
{"x" : "foobar"}
• Date
{"x" : new Date()}
• Regular expression
{"x" : /foobar/i}
• Array
{"x" : ["a", "b", "c"]}
• Embedded document
{"x" : {"foo" : "bar"}}
• Object ID
{"x" : ObjectId()}
• Binary Data
Binary data is a string
of arbitrary bytes.
• Code
{"x" : function() { /* ...
*/ }}
You can download a PDF
version of Mongodb
Interview Questions.

Download PDF

4. How to perform
queries in
MongoDB?
The find method is used to
perform queries in
MongoDB. Querying returns
a subset of documents in a
collection, from no
documents at all to the entire
collection. Which documents
get returned is determined
by the first argument to find,
which is a document
specifying the query criteria.
Example:
> db.users.find({"age" : 24})
5. How do you
Delete a Document?
The CRUD API in MongoDB
provides deleteOne and dele
teMany for this purpose.
Both of these methods take
a filter document as their first
parameter. The filter
specifies a set of criteria to
match against in removing

Mongoose Page 3
match against in removing
documents.

For example:
>
db.books.deleteOne({"_id" :
3})

6. How do you
Update a
Document?
Once a document is stored
in the database, it can be
changed using one of
several update
methods: updateOne, updat
eMany, and replaceOne.
updateOne and updateMany
each takes a filter document
as their first parameter and a
modifier document, which
describes changes to make,
as the second
parameter. replaceOne also
takes a filter as the first
parameter, but as the
second
parameter replaceOne expec
ts a document with which it
will replace the document
matching the filter.
For example, in order to
replace a document:
{
"_id" :
ObjectId("4b2b9f67a1f63173
3d917a7a"),
"name" : "alice",
"friends" : 24,
"enemies" : 2
}
7. How to add data
in MongoDB?
The basic method for adding
data to MongoDB is “inserts”.
To insert a single document,
use the
collection’s insertOne metho
d:
>
db.books.insertOne({"title" :
"Start With Why"})
For inserting multiple
documents into a collection,
we use insertMany. This
method enables passing an
array of documents to the
database.
Start Your Coding Journey
With Tracks
Master Data Structures and
Algorithms
Topic Buckets
Mock Assessments

Mongoose Page 4
Mock Assessments
Reading Material
Earn a Certificate
View Tracks
8. What are some
features of
MongoDB?
• Indexing: It supports
generic secondary
indexes and provides
unique, compound,
geospatial, and full-text
indexing capabilities as
well.
• Aggregation: It
provides an
aggregation framework
based on the concept of
data processing
pipelines.
• Special collection and
index types: It
supports time-to-live
(TTL) collections for
data that should expire
at a certain time
• File storage: It
supports an easy-to-
use protocol for storing
large files and file
metadata.
• Sharding: Sharding is
the process of splitting
data up across
machines.
9. How does Scale-
Out occur in
MongoDB?
The document-oriented data
model of MongoDB makes it
easier to split data across
multiple servers. Balancing
and loading data across a
cluster is done by MongoDB.
It then redistributes
documents automatically.

The mongos acts as a query


router, providing an interface
between client applications
and the sharded cluster.

Config servers store


metadata and configuration
settings for the cluster.
MongoDB uses the config
servers to manage
distributed locks. Each
sharded cluster must have
its own config servers.

Mongoose Page 5
10. What is the
Mongo Shell?
It is a JavaScript shell that
allows interaction with a
MongoDB instance from the
command line. With that one
can perform administrative
functions, inspecting an
instance, or exploring
MongoDB.
To start the shell, run the
mongo executable:
$ mongod
$ mongo
MongoDB shell version:
4.2.0
connecting to: test
>
The shell is a full-featured
JavaScript interpreter,
capable of running arbitrary
JavaScript programs. Let’s
see how basic math works
on this:
> x = 100;
200
> x / 5;
20
11. What are
Databases in
MongoDB?
MongoDB groups collections
into databases. MongoDB
can host several databases,
each grouping together
collections.
Some reserved database
names are as follows:
admin
local
config
12. What is a
Collection in
MongoDB?
A collection in MongoDB is a
group of documents. If a
document is the MongoDB
analog of a row in a
relational database, then a
collection can be thought of
as the analog to a table.
Documents within a single
collection can have any
number of different
“shapes.”, i.e. collections
have dynamic schemas.
For example, both of the

Mongoose Page 6
For example, both of the
following documents could
be stored in a single
collection:
{"greeting" : "Hello world!",
"views": 3}
{"signoff": "Good bye"}
Discover your path to
a Successful Tech Career!
Answer 4 simple questions &
get a career plan tailored for
you
Interview Process
CTC & Designation
Projects on the Job
Referral System
Try It Out
13. What is a
Document in
MongoDB?
A Document in MongoDB is
an ordered set of keys with
associated values. It is
represented by a map, hash,
or dictionary. In JavaScript,
documents are represented
as objects:
{"greeting" : "Hello world!"}
Complex documents will
contain multiple key/value
pairs:
{"greeting" : "Hello world!",
"views" : 3}
MongoDB
Intermediate
Interview
Questions
1. How is Querying
done in MongoDB?
The find method is used to
perform queries in
MongoDB. Querying returns
a subset of documents in a
collection, from no
documents at all to the entire
collection. Which documents
get returned is determined
by the first argument to find,
which is a document
specifying the query criteria.
For example: If we have a
string we want to match,
such as a "username" key
with the value "alice", we use
that key/value pair instead:
>
db.users.find({"username" :
"alice"})
2. Explain the SET
Modifier in
MongoDB?
If the value of a field does
not yet exist, the "$set" sets

Mongoose Page 7
not yet exist, the "$set" sets
the value. This can be useful
for updating schemas or
adding user-defined keys.
Example:
> db.users.findOne()
{
"_id" :
ObjectId("4b253b067525f35f
94b60a31"),
"name" : "alice",
"age" : 23,
"sex" : "female",
"location" : "India"
}
To add a field to this, we use
“$set”:
>
db.users.updateOne({"_id" :
ObjectId("4b253b067525f35f
94b60a31")},
... {"$set" : {"favorite book" :
"Start with Why"}})
3. Explain the
process of
Sharding.
Sharding is the process of
splitting data up across
machines. We also use the
term “partitioning” sometimes
to describe this concept. We
can store more data and
handle more load without
requiring larger or more
powerful machines, by
putting a subset of data on
each machine.
In the figure below, RS0 and
RS1 are shards. MongoDB’s
sharding allows you to create
a cluster of many machines
(shards) and break up a
collection across them,
putting a subset of data on
each shard. This allows your
application to grow beyond
the resource limits of a
standalone server or replica
set.

Sharded Client Connection

Mongoose Page 8
Sharded Client Connection

Non Sharded Client


Connection
4. What are
Geospatial Indexes
in MongoDB?
MongoDB has two types of
geospatial indexes:
2dsphere and 2d. 2dsphere
indexes work with spherical
geometries that model the
surface of the earth based
on the WGS84 datum. This
datum model the surface of
the earth as an oblate
spheroid, meaning that there
is some flattening at the
poles. Distance calculations
using 2sphere indexes,
therefore, take the shape of
the earth into account and
provide a more accurate
treatment of distance
between, for example, two
cities, than do 2d indexes.
Use 2d indexes for points
stored on a two-dimensional
plane.
2dsphere allows you to
specify geometries for
points, lines, and polygons in
the GeoJSON format. A
point is given by a two-
element array, representing
[longitude, latitude]:
{
"name" : "New York City",
"loc" : {
"type" : "Point",
"coordinates" : [50, 2]
}
}
A line is given by an array of
points:
{
"name" : "Hudson River",
"loc" : {
"type" : "LineString",
"coordinates" : [[0,1],
[0,2], [1,2]]
}
}
5. Explain the term
“Indexing” in
Mongoose Page 9
“Indexing” in
MongoDB.
In MongoDB, indexes help in
efficiently resolving queries.
What an Index does is that it
stores a small part of the
data set in a form that is
easy to traverse. The index
stores the value of the
specific field or set of fields,
ordered by the value of the
field as specified in the
index.
MongoDB’s indexes work
almost identically to typical
relational database indexes.
Indexes look at an ordered
list with references to the
content. These in turn allow
MongoDB to query orders of
magnitude faster. To create
an index, use
the createIndex collection
method.
For example:
> db.users.find({"username":
"user101"}).explain("executio
nStats")
Here, executionStats mode
helps us understand the
effect of using an index to
satisfy queries.
MongoDB
Advanced
Interview
Questions
1. What do you
mean by
Transactions?
A transaction is a logical unit
of processing in a database
that includes one or more
database operations, which
can be read or write
operations. Transactions
provide a useful feature in
MongoDB to ensure
consistency.

MongoDB provides two APIs


to use transactions.
• Core API: It is a similar
syntax to relational
databases (e.g.,
start_transaction and
commit_transaction)
• Call-back API: This is
the recommended
approach to using
transactions. It starts a
transaction, executes
the specified
operations, and
commits (or aborts on

Mongoose Page 10
commits (or aborts on
the error). It also
automatically
incorporates error
handling logic for
"TransientTransactionE
rror"
and"UnknownTransacti
onCommitResult".
2. What are
MongoDB Charts?
MongoDB Charts is a new,
integrated tool in MongoDB
for data visualization.

MongoDB Charts offers the


best way to create
visualizations using data
from a MongoDB database.
It allows users to perform
quick data representation
from a database without
writing code in a
programming language such
as Java or Python.

The two different


implementations of
MongoDB Charts are:
• MongoDB Charts PaaS
(Platform as a Service)
• MongoDB Charts
Server
3. What is the
Aggregation
Framework in
MongoDB?
• The aggregation
framework is a set of
analytics tools within
MongoDB that allow
you to do analytics on
documents in one or
more collections.
• The aggregation
framework is based on
the concept of a
pipeline. With an
aggregation pipeline,
we take input from a
MongoDB collection
and pass the
documents from that
collection through one
or more stages, each of
which performs a
different operation on
its inputs (See figure
below). Each stage
takes as input whatever
the stage before it
produced as output.
The inputs and outputs
for all stages are
documents—a stream of
documents.

Mongoose Page 11
documents.

4. Explain the
concept of pipeline
in the MongoDB
aggregation
framework.
An individual stage of an
aggregation pipeline is a
data processing unit. It takes
in a stream of input
documents one at a time,
processes each document
one at a time, and produces
an output stream of
documents one at a time
(see figure below).

5. What is a Replica
Set in MongoDB?
To keep identical copies of
your data on multiple
servers, we use replication. It
is recommended for all
production deployments. Use
replication to keep your
application running and your
data safe, even if something
happens to one or more of
your servers.
Such replication can be
created by a replica set with
MongoDB. A replica set is a
group of servers with one
primary, the server taking
writes, and multiple
secondaries, servers that
keep copies of the primary’s
data. If the primary crashes,
the secondaries can elect a
new primary from amongst
themselves.
6. Explain the
Replication
Architecture in
MongoDB.
The following diagram
depicts the architecture
diagram of a simple replica
set cluster with only three
server nodes – one primary
node and two secondary
nodes:

Mongoose Page 12
• In the preceding model,
the PRIMARY database
is the only active replica
set member that
receives write
operations from
database clients. The
PRIMARY database
saves data changes in
the Oplog. Changes
saved in the Oplog are
sequential—that is,
saved in the order that
they are received and
executed.
• The SECONDARY
database is querying
the PRIMARY database
for new changes in the
Oplog. If there are any
changes, then Oplog
entries are copied from
PRIMARY to
SECONDARY as soon
as they are created on
the PRIMARY node.
• Then, the
SECONDARY
database applies
changes from the Oplog
to its own datafiles.
Oplog entries are
applied in the same
order they were
inserted in the log. As a
result, datafiles on
SECONDARY are kept
in sync with changes on
PRIMARY.
• Usually, SECONDARY
databases copy data
changes directly from
PRIMARY. Sometimes
a SECONDARY
database can replicate
data from another
SECONDARY. This
type of replication is
called Chained
Replication because it
is a two-step replication
process. Chained
replication is useful in
certain replication
topologies, and it is
enabled by default in
MongoDB.
7. What are some
utilities for backup
Mongoose Page 13
utilities for backup
and restore in
MongoDB?
The mongo shell does not
include functions for
exporting, importing, backup,
or restore. However,
MongoDB has created
methods for accomplishing
this, so that no scripting work
or complex GUIs are
needed. For this, several
utility scripts are provided
that can be used to get data
in or out of the database in
bulk. These utility scripts are:
• mongoimport
• mongoexport
• mongodump
• mongorestore
Conclusion
1. Conclusion
MongoDB is a powerful,
flexible, and scalable
general-purpose database. It
combines the ability to scale
out with features such as
secondary indexes, range
queries, sorting,
aggregations, and geospatial
indexes.
Thus, in conclusion,
MongoDB is:
• Supports Indexing
• Designed to scale
• Rich with Features
• High Performance
• Load Balancing
• Supports sharding
Although MongoDB is
powerful, incorporating many
features from relational
systems, it is not intended to
do everything that a
relational database does.
For some functionality, the
database server offloads
processing and logic to the
client-side (handled either by
the drivers or by a user’s
application code). Its
maintenance of this
streamlined design is one of
the reasons MongoDB can
achieve such high
performance.

Mongoose Page 14
Mongoose
Interview
Questions and
Answers
Here are 20 commonly
asked Mongoose
interview questions
and answers to
prepare you for your
interview:
1. What is Mongoose?
Mongoose is a
MongoDB object
modeling tool
designed to work in an
asynchronous
environment.
Mongoose supports
both promises and
callbacks.
2. Can you explain how to
connect to a MongoDB
using Mongoose?
In order to connect to
a MongoDB using
Mongoose, you will
need to first install the
Mongoose npm
package. Once you
have done that, you
can use the
Mongoose.connect()
function to connect to
your MongoDB
database.
3. Can you give me an
example of how you
would define a schema in
Mongoose?
A schema in
Mongoose is simply a
representation of the
structure of your data.
For example, if you
were creating a
schema for a blog
post, it might look
something like this:
“`
var blogSchema = new
mongoose.Schema({
title: String,
body: String,
date: { type: Date,
default: Date.now },
comments: [{ type:
mongoose.Schema.Ty
pes.ObjectId, ref:
‘Comment’ }]
Mongoose Page 15
‘Comment’ }]
});
“`
This schema defines a
few key things about
our data: that it will
have a title and body
(both strings), a date
(with a default value of
the current date and
time), and an array of
comments (which are
ObjectIds that
reference the
Comment model).
4. How do the Schema
and Model objects work
together?
The Schema object
contains information
about the structure of
the data in the
collection, while the
Model object contains
functions that allow
you to interact with the
data in the collection.
5. How can we use
mongoose models to
create, read, update, and
delete documents from
our database?
We can use mongoose
models to create, read,
update, and delete
documents from our
database by using the
model functions
create(), find(),
update(), and
deleteOne().
6. How do we specify
validation rules for data
fields when defining our
schema?
We can specify
validation rules for
data fields by adding
validation keywords to
the field definition in
our schema. For
example, we could add
the “required” keyword
to a field to make sure
that it is always
populated with a value.
7. In what situations is it
best to use a Mongoose
virtual field over a normal
one?
A Mongoose virtual
field is best used when

Mongoose Page 16
field is best used when
you want to define a
field that is not actually
stored in the MongoDB
database, but that you
want to be able to
access and manipulate
as if it were a normal
field. This can be
useful for computed
fields, or for fields that
you want to be able to
populate from an
outside source.
8. What’s the difference
between static methods
and instance methods in
Mongoose?
Static methods are
methods that are
called on the model
itself, while instance
methods are methods
that are called on
documents that are
retrieved from the
database. For
example, you might
have a static method
on the User model
called findByEmail that
takes an email
address and returns
the user with that
email address. An
instance method might
be one that returns the
user’s full name.
9. What are some ways
to validate Mongoose
schemas?
There are several
ways to validate
Mongoose schemas.
One way is to use the
built-in validators that
Mongoose provides,
such as the “required”
validator. Another way
is to use custom
validation functions.
Finally, you can also
use a third-party
validation library like
Validator.js.
10. What are middleware
functions in Mongoose?
Middleware functions
in Mongoose are
functions that are run
before or after certain

Mongoose Page 17
before or after certain
operations are
executed. For
example, you could
use a middleware
function to run some
code before a
document is saved to
the database. This
would allow you to do
things like validate the
data or perform some
other operation on it
before it is actually
stored.
11. How does Mongooe
handle concurrency
issues with multiple users
writing to the same
document?
Mongoose uses a
technique called
“optimistic
concurrency” to handle
this issue. What this
means is that when a
user tries to save a
document that has
been modified by
another user,
Mongoose will first
check to see if the
current version of the
document in the
database is the same
as the version that the
user started with. If it
is, then the changes
are saved. If it isn’t,
then an error is thrown
and the changes are
not saved.
12. What is your opinion
on the performance of
Mongoose?
I believe that
Mongoose is a great
tool for creating quick
and easy prototypes.
However, for
production
applications, I believe
that there are better
options out there in
terms of performance.
13. What are pre-hooks
and post-hooks in
Mongoose?
Pre-hooks and post-
hooks are functions
that are run before and

Mongoose Page 18
that are run before and
after certain methods
are called on
Mongoose models,
respectively. For
example, you could
use a pre-hook to
encrypt a password
before it is saved to
the database, or use a
post-hook to send a
confirmation email
after a new user is
created.
14. Why should I use
Mongoose over raw
MongoDB queries?
Mongoose provides a
schema-based
solution to modeling
your data, which
means that you can
define types and
validations for your
data that will be
enforced when you try
to insert or update
documents. This can
help to keep your data
consistent and avoid
errors. Additionally,
Mongoose provides a
number of helpful
features, like pre- and
post- hooks, that can
make working with
data easier.
15. What’s the difference
between findOne() and
findById()?
The findOne() function
will return the first
document that
matches the query.
The findById() function
will return the
document with the
specified id.
16. Is it possible to have
more than one model per
collection with
Mongoose? If yes then
why would you want to do
that?
Yes, it is possible to
have more than one
model per collection
with Mongoose. There
are a few reasons why
you might want to do
this:
– You might want to
Mongoose Page 19
– You might want to
have different models
for different purposes.
For example, you might
have a “User” model for
storing information
about users, and a
“Post” model for storing
information about posts.
– You might want to
have different models
for different parts of
your application. For
example, you might have
a ” frontend” model for
storing information that
will be used in the
frontend of your
application, and a ”
backend” model for
storing information that
will be used in the
backend of your
application.
– You might want to
have different models
for different versions of
your application. For
example, you might have
a ” v1″ model for storing
information about the
first version of your
application, and a ” v2″
model for storing
information about the
second version of your
application.
17. Can you explain what
populate() is used for in
Mongoose?
The populate() method
in Mongoose is used
to automatically
populate the
referenced fields in a
document with the
data from the
referenced document.
This is useful when
you want to retrieve
data from multiple
documents in a single
query.
18. What is the purpose
of $where in Mongoose
queries?
The $where operator

Mongoose Page 20
The $where operator
in Mongoose queries
allows you to execute
arbitrary JavaScript
expressions to query
for documents. This
can be useful if you
need to query for
documents based on
complex criteria that
can’t be easily
expressed using the
other Mongoose query
operators. However,
you should be aware
that using $where can
be very slow, since it
has to execute the
JavaScript expression
for every document in
the collection.
19. Can you give some
examples of common
query operators in
Mongoose?
Some common query
operators in Mongoose
are $gt (greater than),
$lt (less than), $eq
(equal to), $ne (not
equal to), $in (in), and
$nin (not in). These
operators can be used
to construct queries
that filter documents
based on specific
criteria.
20. What are some tips to
keep in mind while
designing Mongoose
Schemas?
1. Keep your schemas
as simple as
possible – only include
the fields that you
absolutely need.
2. Make use of
Mongoose’s built-in
features, like validation
and typecasting.
3. Use virtuals to avoid
duplication of data.
4. Use pre and post
hooks to add extra
functionality to your
schemas.

Mongoose Page 21
Top MongoDB
Interview
Questions and
Answers

1) What is MongoDB?
MongoDB is a cross-
platform document-based
database. Categorized as a
NoSQL database,
MongoDB avoids the
conventional table-oriented
relational database
structure in support of the
JSON-like documents with
the dynamic schemas,
making the data integration
in specific kinds of
applications quicker and
simpler.

MongoDB was developed


by a software company
“10gen”, in October 2007
as an element of the
planned platform as the
service product. After that,
the company was shifted to
a freeware deployment
model in 2009, providing
sales assistance and other
services.

2) What are the


features of MongoDB?
Following are the important
features of MongoDB:

• A compliant data model in


the format of documents.
• Agile and extremely
scalable database.
• Quicker than traditional
databases.
• Demonstrative query
language.
Check out MongoDB
Tutorial

3)What type of NoSQL


database MongoDB

Mongoose Page 22
database MongoDB
is?
MongoDB is a document-
oriented database. It stores
the data in the form of the
BSON structure-oriented
databases. We store these
documents in a collection.

4) Explain
Namespace?
A namespace is the series
of the collection name and
database name.

Basic MongoDB
Interview
Questions And
Answers

5)Differentiate
MongoDB and
MySQL?
Despite MySQL and
MongoDB being freeware
and open source
databases, there are
several differences
between them in terms of a
data relationship,
transaction, performance
speed, querying data,
schema design,
normalization, etc. The
comparison between
MongoDB and MySQL is
similar to the comparison
between Non-relational and
Relational databases.

Check out the Related


Article MongoDB
Vs MySQL

6) Explain Indexes in
MongoDB?
In MongoDB, we
use Indexes for executing
the queries efficiently;
without using Indexes,
MongoDB should carry out
a collection scan, i.e., scan
all the documents of a
collection, for selecting the
documents which match
the query statement. If a
suitable index is available
for a query, MongoDB will
use an index for restricting
the number of documents it
should examine.

7) Why MongoDB is

Mongoose Page 23
7) Why MongoDB is
the best NoSQL
database?
MongoDB is the best
NoSQL database due to
the following features:

• High Performance
• High Availability
• Easily Scalable
• Rich Query Language
• Document Oriented

8) Explain the
significance of the
covered query?
A covered query makes the
query implementation
quicker as we store the
indexes in the RAM or
consecutively located on
the disk. It makes query
execution quicker. The
covered query covers all
the fields in the index,
MongoDB matches the
query condition along with
returning the result fields.

9) What is a replica
set?
We can specify the replica
as a set of the mongo
instances which host a
similar data set. In the
replica set, one node will
be primary, and another
one will be secondary. We
replicate all the data from
the primary to the
secondary nodes.

10) Differentiate
MongoDB and
Cassandra?

MongoDB Cassandra
It is a cross- It is a high-
platform performanc
document- e distributed
oriented database
database system.
system
It is It is
developed in developed
C++ in Java
It is simple It offers high

Mongoose Page 24
It is simple It offers high
to administer availability
in the failure
case

11) Explain the primary and


secondary replica set?

In MongoDB, primary
nodes are the nodes that
accept writing. Primary
nodes are also called
master nodes. Replication
in MongoDB is a single
master. Therefore, only one
node will accept the write
operations at once.

12) Which languages


can we use with
MongoDB?

At Present, MongoDB
offers driver support to
C++, Java, PHP,
Perl, Python, Go, Scala,
and Ruby.

Check out Cassandra vs


MongoDB

13) Explain Storage


Encryption?
Storage encryption
encodes all the MongoDB
data over the storage or
over the operating systems
for assuring that only
authenticated processes
will access the safeguarded
data.

14) Explain Primary


and Secondary
Replica Sets?
Primary Replica Set
receives all the write
operations from the clients.
Secondary replica sets
replicate the primary replica
sets and implement the
operations for their
datasets so that secondary
datasets affect the primary
datasets.

15) What is the


importance of GridFS
and Journaling?
• GridFS: We use GridFS to
retrieve and store large
files like images, videos,
and audio files.
• Journaling: We use
Journaling for secure
backups in MongoDB.

Mongoose Page 25
backups in MongoDB.

16) How to do locking


or transactions in
MongoDB?
MongoDB does not use
traditional locking with the
reduction because it is
high-speed, knowable, and
light in the presentation.
We can consider it as the
MyISAM, MySQL auto
entrust script. Through the
simpler business sustain,
we can enhance the
performance, specifically in
the structure with various
servers.

17) How to do
Journaling in
MongoDB?
We save the write
operations in the memory
while journaling is taking
place. The on-disk journal
files are dependable for the
reason that journal writers
are usual. In the DB path,
MongoDB designs a journal
subdirectory.

18) How does


MongoDB provides
concurrency?
MongoDB utilizes the
reader-writer locks,
enabling concurrent
readers to access any
supply such as collection or
database though it provides
private access to individual
writers.

19) Explain Sharding


and Aggregation in
MongoDB?
• Aggregation: Aggregations
are the activities that
handle the data records
and give the record results.
• Sharding: Sharding means
storing the data on multiple
machines.

20) What is the


importance of profiler
in MongoDB?
MongoDB contains the
database profiler that
shows the performance
characteristics of every
operation against the
database. Through the

Mongoose Page 26
database. Through the
profiler, we can identify the
queries that are slower
than they should be and
use this data to determine
when we require an index.

21) Define Collection?


The collection is a set of
MongoDB documents.

22) Explain
Aggregation Pipeline?
The aggregation Pipeline
acts as a framework to
perform aggregation tasks.
We use this pipeline for
transforming the
documents into aggregated
results.

23) Explain
MapReduce?
MapReduce is a standard
multi-phase data
aggregation modality that
we use to process the data
quantities.

24) Explain Splitting?


Splitting is the background
process that we use to
store chunks from
increasing too large.

25) What is the


purpose of the save()
method?
We use the save() method
for replacing the existing
documents with new
documents.

26) What is the use of


MongoDB?
• Generally, we use
MongoDB as the main data
store for the operational
requirements with live
needs. Generally,
MongoDB is suitable for
80% of the applications
which we develop today.
MongoDB is simple to
operate and extent in ways
that are tough if they are
not possible with the
relational databases.
• MongoDB stands out in
various use cases where
the relational databases
are not suitable, like
applications with semi-
structured, structured,

Mongoose Page 27
structured, structured,
along with the big
scalability needs or the
multi-datacenter
deployments.
• MongoDB cannot be
suitable for some
applications. For instance,
applications that need
complex transactions and
scan-based applications
that access huge subsets
of the data largely cannot
be suitable for MongoDB.
• Some general uses of
MongoDB comprise
product catalogs, mobile
apps, content
management, real-time
personalization, and
applications providing
individual views throughout
several systems.

27) What is the


purpose of the DB
command?
We use the “DB” command
to get the name of the
presently selected
database.

28) What are the


restrictions of the
MongoDB 32-bit
versions?
When we run a 32-bit
version of MongoDB, the
total storage size of the
server, containing indexes
and data, is 2GB. Due to
this reason, we will not
deploy MongoDB to the
production on the 32-bit
machines. If we deploy a
64-bit version of MongoDB,
there is no virtual restriction
to the storage size. For the
creation deployments, we
strongly recommend 64-bit
operating systems and
builds.

29) When should we


normalize the data in
MongoDB?
It relies on our objectives.
Normalization provides an
updated effective data
representation.
Denormalisation makes
data reading effective.
Generally, we utilize
embedded data models
when:

• When we have “contains”

Mongoose Page 28
• When we have “contains”
relationships between the
entities.
• When we have one-to-
many relationships
between the entities. In the
relationships, “many” or
the child documents display
in the context of the parent
documents.

Generally, we use
normalized data models:

• When embedding results in


duplication of the data yet
they will not give enough
read performance
advantages to prevail the
duplication implications.
• For representing more
difficult many-to-many
relationships.
• For modeling the big
hierarchical data sets.

30) How do we
perform sorting and
Explain Project in
MongoDB?
For finding any data in
MongoDB, we use the
find() method. The
discovery () method returns
the collection’s documents
over which we invoked this
method. We can use the
“Where” clause in the
MongoDB query in order to
restrict the output by using
MongoDB projection.
Anytime we execute the
find() method, MongoDB
returns all the documents
associated with a particular
collection.

db.<collection_name>.find({ },
{<key_Name>:<Flag to display>})

31) How can


MongoDB simulate
subquery or join?
We have to find the best
method for structuring the
data in MongoDB for
simulating what would be
the simple subquery or join
in SQL. For example, we
have users and posts, with
the users in one collection
and posts in another
collection. We have to find
all the posts by the users
whose city is “Hyderabad”.

32) Define
oplog(operational log)?

Mongoose Page 29
An operational log (oplog)
is a special kind of limited
collection that stores a
rolling record of all the
operations which change
the data we store in our
databases. Primarily, it
applies all the database
operations over the primary
and, after that, records
these operations on the
oplog of the primary. After
that, the secondary
members replicate and
apply the operations in the
asynchronous process.

33) How do we create


a database in
MongoDB?
When I want to create a
database in MongoDB, I
faced the following error:

:~$mongo
MongoDB shell version:1.65
Connecting to: test
Error: Could not connect to the
server
Exception: connect failed

The solution to the above


error:

1. cd/var1/lib1/MongoDB
2. We remove the mongod.
lock from the folder
3. Sudo start MongoDB
4. Mongo

34) What is the syntax


of the skip() method?
skip() method syntax is:

db.COLLECTION_NAME.find().limit
(NUMBER).skip(NUMBER)

35) How do we delete


everything from the
MongoDB database?
By using the following
code, we can delete
everything from the
MongoDB database:

use [database];
db.dropDatabase();
Ruby code should be pretty
similiar.
Also, from the command line:
mongo [Database] -eval
"db.dropDatabase();"
use
[databaseName]
db.Drop+databasename();
drop colllection
use databaseName
db.collectionName.drop();

Mongoose Page 30
db.collectionName.drop();

36) Which command


do we use for creating
the backup of the
database?
We use the mongodump
command for creating the
database backup.

37) Which command


do we use for restoring
the backup?
We use mongorestore for
restoring the backup.

38) Explain the


importance of the dot
notation?
In MongoDB, we use dot
notation for accessing the
array elements and the
fields of an embedded
document.

39) What is the syntax


of the limit() and sort()
method?
Syntax of the limit() method
is:

>
db.COLLECTION_NAME.find().limit
(NUMBER)

Syntax of the sort()


method is:

>
db.COLLECTION_NAME.find().sort
({KEY:1})

40) What do you know


about NoSQL
databases? What are
the various types of
NoSQL databases?
NoSQL refers to “Not Only
SQL”. NoSQL is a kind of
database that handles and
sorts all kinds of structured,
massive, and difficult data.
It is a new method to think
about databases. Kinds of
NoSQL databases:

• Key-Value
• Graph
• Column Oriented
• Document Oriented

41) Which command


do we use for dropping

Mongoose Page 31
do we use for dropping
a database?
We use the “DB.drop
database” command for
dropping a database.

42) Explain MongoDB


Projection
In MongoDB, we use
Projection for selecting only
the required data. It will not
select the complete data of
a document.

43) Why do we use the


pretty() method?
We use the pretty() method
for displaying the results in
a formatted way.

44) How do we
remove a document
from the collection?
By using the remove()
method, we remove a
document from the
collection.

45) What are the


points we should
consider while creating
a schema in
MongoDB?
We must consider the
following points while
creating a schema:

• Designing the Scheme


based on the user
requirements.
• Combining the objects into
one document, if we have
to use them jointly, or else,
separate them.
• Perform joins while on
write, and not while it is
reading.
• For most general
application scenarios,
maximize the schema.
• Perform complex
aggregations in the
schema.

46) What does


ObjectId contain?
ObjectId contains the
following:

• Client machine ID
• Client process ID
• Byte incremented counter
• Timestamp

Mongoose Page 32
• Timestamp

47) How do we use the


select * group by
MongoDB
aggregation?
For instance, if we have to
select all the attributes and
groups by name throughout
the records. For example:

{Name: George, x: 5, y: 3}
{Name: George, z: 9}
{Name: Rob, x: 12, y: 2}

We can do MongoDB
aggregation as follows:

db.example.aggregate(
{
$group:{
_id:'$name',
x: {$addToSet: "$x" },
y: {$addToSet: "$y" },
z: {$addToSet: "$z" },
}
}
)

48) Explain Vertical


Scaling and Horizontal
Scaling?
• Vertical Scaling: Vertical
Scaling increases storage
and CPU resources for
expanding the capacity.
• Horizontal
Scaling: Horizontal Scaling
splits the datasets and
circulates the data over
multiple shards or servers.

49) What are the


elements of the
Sharded Cluster?
Following are the elements
of the Sharded Cluster:

• Query routers
• Shards
• Config servers

50) What are the


substitutes for
MongoDB?
Following are the
substitutes to MongoDB:

• Hbase
• CouchDB
• Cassandra
• Redis
• Riak

51) How can we old


files in the moveChunk
Mongoose Page 33
files in the moveChunk
directory?
In the course of general
shard balancing operations,
we make the old files as
backups, and we can
delete them when those
operations are completed.

52) What is a Storage


Engine?
Storage Engine is a
component of the database
that is accountable to
manage how we store on
the disk. For instance, one
storage engine may
provide better performance
for the read-heavy
workloads, and another
one may support a great
throughput for the write
operations.

53) Does MongoDB


require plenty of RAM?
No, MongoDB does not
require plenty of RAM. It
can run on a small amount
of memory. MongoDB
dynamically assigns and
unassigns RAM according
to the needs of other
processes.

Advanced
MongoDB
Interview
Questions And
Answers

54) Differentiate
MongoDB and
CouchDB?

MongoDB CouchDB
MongoDB is CouchDB is
quicker than more secure
CouchDB than
MongoDB
Triggers do Triggers
not exist in exist in
MongoDB. CouchDB
MongoDB CouchDB
serializes does not
the JSON store the
Data to the data in
BSON JSON
format

55) Explain Capped


Collection?

Mongoose Page 34
Collection?
In MongoDB, the Capped
collection is a special kind
of collection. This indicates
that in this collection, we
can restrict the collection
size. Syntax of Capped
Collection is as follows:

db.createCollection(<collection_n
ame>, {capped: Boolean,
autoIndexId: Boolean, size:
Number, max : Number})

In the Capped Collection


syntax, we have the
following fields:

• Collection_Name: This field


is the collection name that
we create as the capped
collection.
• Capped: Capped is a
boolean field; it is true if we
create a capped collection.
By default, its value is
false.
• auto indexed: It is a
boolean flag that we use for
auto-indexing. If this flag is
true, indexes will be
created automatically. If the
flag is false, indexes will
not be created
automatically.
• Size: Size is the parameter
that represents the
maximum amount of
documents in bytes. It is
the required field in the
context of capped
collections.
• Max: Max is the parameter
that represents the highest
number of documents that
permit the collection.

56) How do we
perform the Join
operations in
MongoDB?
From MongoDB3.2, we can
perform the Join operation.
The new $lookup operator
included with the
aggregation pipeline is the
same as the left outer join.
Example:

{
$lookup:
{
from: <collection to join>,
localField: <field from the
input documents>,
foreignField: <field from the
documents of the "from"
collection>,
as: <output array field>
}

Mongoose Page 35
}
}

57) What are the


storage engines used
by MongoDB?
WiredTiger and MMAPv1
are the two storage
engines used by MongoDB.

58) How do we
configure the cache
size in MongoDB?
In MongoDB, we cannot
configure the cache.
MongoDB utilizes the free
spaces over the system
automatically by using
memory-mapped files.

59) How do we control


the MongoDB
Performance?
We can control the
MongoDB Performance by:

• Locking the Performance


• Identifying the number of
connections
• Database Profiling
• Full-time Diagnostic Data
Capture

60) What are the


aggregate functions of
MongoDB?
Following are the
aggregate functions of
MongoDB:

• AVG
• Sum
• Min
• Max
• First
• Push
• addTo Set
• Last

61) What are the


CRUD operations of
MongoDB?
Following are the CRUD
operations of MongoDB:

Create-db.collection.insert();
Read-db.collection.find();
Update-db.collection.update();
Delete-db.collection.remove();

62) What are the


datatypes of
MongoDB?

Mongoose Page 36
MongoDB?
Following are the datatypes
of MongoDB:

• Integer
• String
• Boolean
• Array
• Double
• Date
• Timestamp
• Regular Expression

63) Is it required to
invoke “get last error”
for making a write
durable?
No, it is not required to
invoke “get last error”. The
server acts as if it has been
invoked. “get last error”
enables us to acquire
confirmation that a write
operation is committed.
You will get the
confirmation, yet the
durability and safety of the
writer are independent.

64) What happens


when the Shard is
slow or down while
querying?
When the Shard is slow,
the query returns an error
until partial query options
are fixed. When the shard
is reacting slowly,
MongoDB waits for it.

65) How do we use a


primary key in
MongoDB?
“_id field” is reticent for a
primary key in MongoDB.
And it is a distinct value. If
we do not set anything to
the “_id”, it will
systematically fill it with the
“MongoDB Id Object”. Yet,
we can store any distinct
information in that field.

66) How do we see the


connections utilized by
MongoDB?
For seeing the connections
utilized by MongoDB, we
use db_adminCommand(”
connPoolStats”).

67) When a
“moveChunk” fails, is it

Mongoose Page 37
“moveChunk” fails, is it
required to clean up
partly moved docs?
No, it is not required to
clean up the partly moved
docs because chunk
moves are deterministic
and consistent. The move
will try again, and when
finished, data will be on the
latest Shard.

68) Explain how to


start the MongoDB
Instance or Server?
We have to follow the
below steps for starting the
MongoDB Server:

• First, open the command


prompt and execute the
“mongod.exe” file.
• On the other hand, we
move to the path where we
installed MongoDB.
• Go to the bin folder, find
the “mongod.exe” file, and
double click the file for
executing it.
• We can go to the folder, for
instance, “C: MongoDB/bin”
and type mongo for
connecting MongoDB by
using the Shell.

69) Differences
between MongoDB
and RDBMS

Basis Mongo RDBM


for DB S
Comp
arison
Definiti It is a It is a
on non- relation
relation al
al databas
databas e
e manage
ment
system
Workin It works It is a
g over docume
relation nt-
ships oriented
among databas
the e
tables, system
which through
use fields
columns and
and docume
rows nts
Scalabi It is It is
lity horizont verticall
ally and y
verticall scalable

Mongoose Page 38
verticall scalable
y
scalable
Perfor Perform Perform
mance ance ance
enhanc enhanc
es with es with
the rise the rise
in the in the
process RAM
ors capacity
Hierarc It has a It is
hical built-in hard to
Data provisio store
Storag n to the
e store hierarch
the ical data
hierarch
ical data
Suppor It does It
t to not support
Joins support s
difficulty complex
Joins joins
Query It uses It uses
Langua BSON SQL to
ge for query
databas the
e databas
queryin e
g
Javasc It It does
ript provide not
Suppor s provide
t support support
to to the
javascri javascri
pt- pt-
based based
clients clients
for to query
queryin the
g the databas
databas e
e

70) How do
applications access
the real-time data
modifications in
MongoDB?
Applications access the
real-time data modifications
through the Change
streams that serve as the
subscriber for every
collection operation like
delete, insert, and update.

71) What are the


different kinds of
Indexes in MongoDB?
Following are the different
kinds of Indexes in
MongoDB:

Mongoose Page 39
• Default: It is the “_id” that
MongoDB creates.
• Compound: It is useful for
multiple fields.
• Multi-key: It indexes the
array data.
• Single field: It sorts and
indexes over a single field.
• Geospatial: It is useful for
querying the location data.
• Hashed: It indexes the
hashes of the multiple
fields.

MongoDB
Interview
Questions And
Answers For
Experienced

71) Define BSON?


Binary JSON or BSON is a
binary-encoded format of
the JSON. BSON extends
the JSON and offers
various data fields and
types.

72) How does


MongoDB store the
data?
As it is a document-based
database, MongoDB stores
the documents in Binary
Javascript Object Notation
or BSON, which is a binary-
encoded format of JSON.

73) Does MongoDB


support ACID
Transaction? Define
ACID Transaction?
Yes, MongoDB supports
ACID Transaction. ACID
refers to Atomicity,
Consistency, Isolation, and
Durability. Transaction
manager assures that we
handle these attributes.

74) Explain
Composing elements
or Structure of
ObjectID in MongoDB?
In MongoDB, ObjectID is
associated with the “_id”
field, and MongoDB uses it
as the default value of the
“_id” in the documents. For
generating “ObjectID”, we
use the following Syntax:

Mongoose Page 40
ObjectId([SomeHexaDecimalValue
])

Example:

ObjectId() = newObjectId

ObjectID has the following


methods:

• Str: This method provides


the string representation of
the object id.
• valueOf()- This method
returns hexadecimal
representation of the
ObjectId.
• getTimeStamp()- This
method returns timestamp
of the ObjectId.
• toString()- This method
returns the string
representation of the
ObjectId in
“ObjectId(haxstring)”.

75) How do we find


array elements with
multiple criteria?
For example, if we have the
below documents:

{ _id: 1, numbers: [1000, -1000]]


{ _id: 2, numbers: [500]]

When we execute the


following command:

db.example.find( { numbers:
{ $elemMatch: { $gt: -10, $lt:
10 } } } );

76) How can we sort


the user-defined
function? For example,
x and y are integers,
and how do we
calculate “x-y”?
By executing the following
code, we calculate x-y.

db.eval(function() {
return
db.scratch.find().toArray().sort(fu
nction(doc1, doc2) {
return doc1.a – doc2.a
})
});

Versus the equivalent client-side


sort:
db.scratch.find().toArray().sort(fu
nction(doc1, doc2) {
return doc1.a – doc2.b
});

By using the aggregation


pipeline and “$orderby”
operator, it is possible to
sort.

Mongoose Page 41
sort.

77) Upto Which extent


does the data expand
to multi-slice?
MongoDB shred stands on
the collection. Therefore,
we store all the substances
in a mass or a lump. When
we have an additional time
slot, then we will have few
slice data achievement
options, yet when we have
multiple lumps, data will be
extended to numerous
slices.

78) How do we
retrieve MongoDB
databases in
Javascript Array?
In the MongoDB terminal,
we can run “Show DBS” to
retrieve the existing
databases. To get the
MongoDB databases
programmatically, we
execute the following code:

use admin
dbs =
db.runCommand({listDatabases:
1})
dbNames = []
for (var i in dbs.databases)
{ dbNames.push(dbs.databases[i].
name) }
Hopefully this will help someone
else.
The below will create an array of
the names of the database:
var connection = new Mongo();
var dbNames =
connection.getDBNames();

79) How do we update


the object in the
Nested Array?
By executing the following
code, we update the object:

Skip code block


{
“_id” :
ObjectId(“4faaba123412d654fe83
hg876”),
“user_id” : 123456,
“total” : 100,
“items” : [
{
“item_name” : “my_item_one”,
“price” : 20
},
{
“item_name” : “my_item_two”,
“price” : 50
},
{
“item_name” : “my_item_three”,

Mongoose Page 42
“item_name” : “my_item_three”,
“price” : 30
}
]
}

80) How do we
retrieve a particular
embedded document
in a MongoDB
collection?
I have a collection that has
an embedded document
known as notes.

Skip code block


{
“_id” :
ObjectId(“4f7ee46e08403d063ab
0b4f9”),
“name” : “MongoDB”,
“notes” : [
{
“title” : “Hello MongoDB”,
“content” : “Hello MongoDB”
},
{
“title” : “ReplicaSet MongoDB”,
“content” : “ReplicaSet
MongoDB”
}
]
}

81) How do we query


a nested Join?
To query the nested join,
we use “tested”. For
example:

{“_id” : ObjectId( “abcd” ),


“className” : “com.myUser”,
“reg” : 12345,
“test” : [
{ “className” : “com.abc”,
“testid” : “pqrs” } ] }

82) Can we run more


than one Javascript
Operation in one
MongoDB instance?
Yes, we can run multiple
javascript operations in one
MongoDB instance.

Mongoose Page 43
1. What is MongoDB and what
are its main features?
MongoDB is a robust,
document-oriented NoSQL
database designed for high
performance, scalability, and
developer agility.
Key Features
Flexible Data Model
• Employs JSON-like
documents (BSON
format), facilitating
complex data
representation, deep
nesting, and array
structures.
• Provides dynamic
schema support,
allowing on-the-fly data
definition and data
types.
• Permits multi-document
transactions within a
replica set (group of
nodes). Sharding extend
s this to support large
distributed systems.
Indexed Queries
• Offers extensive
indexing capabilities,
such as single and multi-
field
support, text, geospatial
, and TTL (Time to Live)
Indexes for data
expiration.
• Gives developers the
tools needed to design
and optimize query
performance.
High Availability & Horizontal
Scalability
• Uses replica sets for
data redundancy,
ensuring auto-failover in
the event of a primary
node failure.
• Adopts sharding
to distribute data across
clusters, facilitating
horizontal scaling for
large datasets or high-
throughput
requirements.
Advanced Querying
• Engages in ad-hoc
querying, making it easy
to explore and analyze
data.
• Provides aggregation
pipeline, empowering
users to modify and
combine data, akin to
SQL GROUP BY.
• Specialized query tools
like Map-
Reduce and Text

Mongoose Page 44
Reduce and Text
Search cater to
distinctive data
processing needs.
Embedded Data Management
• Encourages a rich,
document-based data
model where you
can embed related
data within a single
structure.
• This denormalization
can enhance read
performance and data
retrieval simplicity.
Rich Tool Suite
• Further augmented by
several desktop and
web-supported clients,
MongoDB Atlas offers a
seamless and unified
experience for database
management.
• Web-based MongoDB
Compass handles query
optimization and
schema design.
Code Sample: Data Interaction
with MongoDB
Here is the Python code:
from pymongo import
MongoClient
client = MongoClient() #
Connects to default address
and port
db =
client.get_database('mydatab
ase')
# Insert a record
collection =
db.get_collection('mycollection
')
inserted_id =
collection.insert_one({'key1':
'value1', 'key2':
'value2'}).inserted_id
# Query records
for record in
collection.find({'key1':
'value1'}):
print(record)
# Update record
update_result =
collection.update_one({'_id':
inserted_id}, {'$set': {'key2':
'new_value'}})
print(f"Modified
{update_result.modified_count
} records")
# Delete record
delete_result =
collection.delete_one({'key1':
'value1'})
print(f"Deleted
{delete_result.deleted_count}
records")
2. How does MongoDB differ
from relational databases?
While both MongoDB and
relational databases handle
data, they do so in
fundamentally different ways.
Let's explore the key
distinctions.

Mongoose Page 45
distinctions.
Data Model
Relational Databases
• Use tables with
predefined schemas
that enforce
relationships and data
types.
• Often use normalization
techniques to minimize
data redundancy.
MongoDB
• Stores data as flexible,
schema-less sets of key-
value pairs inside
documents.
• Relationships can be
represented through
embedded documents
or referencing via keys,
providing more granular
control and allowing for
a more natural
representation of real-
world data.
Data Integrity
Relational Databases
• Rely on ACID
transactions to ensure
data consistency.
MongoDB
• Offers ACID
guarantees at the
document level, though
transactions across
multiple documents
happen within the same
cluster to ensure
consistency.
• Provides multi-
document
transactions for more
complex operations.
Query Language
Relational Databases
• Use SQL,
a declarative query
language.
MongoDB
• Employs JSON-
like queries, which
are imperative and
resemble the structure
of the data it operates
on.
Scalability
Relational Databases
• Traditionally use
a vertical
scaling approach,
featuring limits on a
single server's resources
such as CPU, storage,
and memory.
MongoDB
• Designed for horizontal
scaling, making it easier
to handle larger datasets
and heavier loads by

Mongoose Page 46
and heavier loads by
distributing data across
multiple servers. This
scalability also supports
cloud-based setups.
Performance
Relational Databases
• Can handle complex
queries efficiently but
might require multiple
joins, potentially
degrading performance.
MongoDB
• Optimized for quick
CRUD operations and
can efficiently handle
large volumes of read
and write requests.
Indexing
Relational Databases
• Tables can have a
multitude of indexes,
which can be a mix of
clustered, non-
clustered, unique, or
composite.
MongoDB
• Collections can have
several indexes,
including single field,
compound, and multi-
key indexes.
Data Joins
Relational Databases
• Use joins to merge
related data from
different tables during a
query, ensuring data
integrity.
MongoDB
• Offers embedded
documents and manual
reference to achieve
similar results, but
multi-collection joins
have performance and
scalability
considerations.
3. Can you describe the structure
of data in MongoDB?
In MongoDB, data units are
organized into collections,
which group related
documents.
Each document corresponds
to a single record and maps to
fields or key-value pairs.
JSON-Like Format
Data in MongoDB is stored
using a BSON (Binary JSON)
format that can handle a
maximum depth of 100 levels.
This means a BSON object or
element can be a document
consisting of up to 100 sub-
elements, such as fields or
values.
Example: Nested Document
Here is a nested document:

Mongoose Page 47
Here is a nested document:
{
"_id": "123",
"title": "My Blog Post",
"author": {
"name": "John Doe",
"bio": "Tech enthusiast"
},
"comments": [
{
"user": "Alice",
"text": "Great post"
},
{
"user": "Bob",
"text": "A bit lengthy!"
}
]
}
In the example above, the
"author" field is an embedded
document (or sub-document),
and the "comments" field is an
array of documents.
Key Features
• Ad-Hoc Schema:
Documents in a
collection don't need to
have the same fields,
providing schema
flexibility.
• Atomicity at the
Document Level:
The ACID properties
(Atomicity, Consistency,
Isolation, Durability) of a
transaction, which
guarantee that the
modifications are
successful or
unsuccessful as a unit of
work.
• Index Support:
Increases query
performance.
• Support for Embedded
Data: You can nest
documents and arrays.
• Reference Resolution: It
allows for processing
references across
documents. If a
referenced document is
modified or deleted, any
reference to it from
another document also
needs to be updated or
deleted in a multi-step
atomic operation.
• Sharding and
Replication: For
horizontal scaling and
high availability.
Data Model Considerations
1. One-to-One: Typically
achieved with
embedded documents.
2. One-to-Many (Parent-
Child): This can be
modelled using
embedded documents

Mongoose Page 48
embedded documents
in the parent.
3. One-to-Many
(Referenced): Achieved
through referencing,
where several
documents contain a
field referencing a single
document. For better
efficiency with frequent
updates, consider
referencing.
4. Many-to-Many:
Modeled similarly to
"One-to-Many"
relationships.
5. You should avoid using
“repeatable patterns”,
such as storing data in
separate arrays or
collections, to ensure
smooth data
manipulation and
effective query
operations.
For example, using
separate collections for
similar types of data
based on a category like
"users" and "admins"
instead of a single
"roles" array with
multiple documents.
The above best practice
example prevents data
redundancy and
ensures consistency between
similar documents. Redundant
storage or separating non-
redundant data can lead to
inconsistencies and increase
the effort required for
maintenance.
4. What is
a Document in MongoDB?
In MongoDB, a document is
the basic data storage unit. It's
a JSON-like structure that
stores data in key-value pairs
known as fields.
Document Structure
Each document:
• Is a top-level entity,
analogous to a row in a
relational database.
• Is composed of field-
and-value pairs, where
the value can be a
variety of data types,
including arrays or sub-
documents.
• Has a unique _id or
primary key that is
indexed for fast lookups.
Here is the document
structure:
{
"_id": 1,

Mongoose Page 49
"_id": 1,
"name": "John Doe",
"age": 30,
"email":
"[email protected]",
"address": {
"city": "Example",
"zip": "12345"
},
"hobbies": ["golf", "reading"]
}
Collections
Documents are grouped
into collections. Each
collection acts as a container
with a unique namespace
within a database. Collections
don't enforce a predefined
schema, which allows for
flexibility in data modeling.
Key Advantages
1. Flexibility: Documents
can be tailored to the
specific data needs of
the application without
adherence to a rigid
schema.
2. Data Locality: Related
data, like a user's profile
and their posts, can be
stored in one document,
enhancing performance
by minimizing lookups.
3. JSON Familiarity:
Documents, being JSON-
like, enable easier
transitions between
application objects and
database entities.
4. Indexing: Fields within
documents can be
indexed, streamlining
search operations.
5. Transaction Support:
Modern versions of
MongoDB offer ACID-
compliant, multi-
document transactions
that ensure data
consistency.
Example Use Case
Consider an online library.
Instead of having separate
tables for users, books, and
checkouts as in a relational
database, you could store all
the pertinent data about a
user, including their checked-
out books, in a single
document within
a users collection:
{
"_id": 1,
"name": "John Doe",
"email":
"[email protected]",
"address": { "city":
"Example", "zip": "12345" },
"checkedOutBooks": [
{ "bookId": 101, "dueDate":
"2022-02-28" },

Mongoose Page 50
"2022-02-28" },
{ "bookId": 204, "dueDate":
"2022-03-15" }
]
}
This approach enables swift
retrieval of all pertinent user
information in one go.
Considerations
• Atomicity: While single-
document operations
are atomic by default in
MongoDB, transactions
and atomicity guarantee
apply to multi-document
operations primarily.
• Size Limitations:
Documents can't exceed
16MB in size. In most
cases, this limit should
not be a practical
concern.
5. How is data stored
in collections in MongoDB?
In MongoDB, data is stored
in types of collections,
ensuring flexibility and
efficiency in data modeling.
Collection Basics
• Collections are
the primary data
storage structures in
MongoDB, akin to tables
in relational databases.
• They are schema-less,
meaning that
documents within a
collection can have
varying structures. This
offers superior
flexibility, while still
allowing for structure
validation through the
use of JSON schema.
Documents
• Documents serve as the
unit of data storage in
MongoDB. These are
akin to rows in relational
databases or objects in
languages such as
JavaScript.
• Documents are
represented
in BSON (Binary JSON)
format, a binary
representation closely
mirroring JSON's
attribute-value data
model.
Data Organization Hierarchy
• Data in MongoDB is
organized in
a hierarchical structure,
with each database
having one or
more collections, each
of which stores
multiple documents, all

Mongoose Page 51
multiple documents, all
of which can possess
distinct structures.
Key Data Principles
• MongoDB collections
are designed
to optimize data access
rather than just serving
as containers.
• To maximize efficiency,
it's crucial to design
collections that cater to
common query patterns.
Types of Database Collections
• By understanding the
nuances of each
collection type, you can
better customize your
MongoDB system
to cater to specific use-
cases and performance
requirements.
AJAX Comments
• To effectively and
iteratively store and
manage comments, the
AJAX Comments feature
is engineered to provide
a blend of flexibility and
ease of access.
• It leverages JSON-like
documents and the
native power of
MongoDB, such as rich
indexing for efficient
interactions.
Newsfeed Posts
• Tailored for sequential,
feed-like content, such
as posts from a social
media platform or a
messaging app.
• It benefits greatly from
the ordered nature
of BSON documents,
making sure newer
posts are easy to fetch.
User Profiles
• Focusing on user-
defined, diverse, and
possibly unstructured
details, the User Profile
collection is an ideal
repository for self-
descriptive user profiles.
• The flexibility of schema
allows for
comprehensive storage
with minimal overhead.
Metadata
• For persistent and global
configurations, the
Metadata collection
provides a secure space
to cache system
information.
Product Catalog
• Bolsters browsing and

Mongoose Page 52
• Bolsters browsing and
shopping activities by
housing consistent,
structured details
related to products or
services on offer.
• This attention
to consistency helps in
easy data retrieval and
optimized user
experiences.
Logging
• Ideally suited to record
system interactions and
debugging info, the
Logging collection
maintains an organized
trail of system activity,
nurturing a culture of
informed decision-
making.
6. Describe what a MongoDB
database is.
A MongoDB database is a
document-oriented, NoSQL
database consisting of
collections, each of which in
turn comprise documents.
Core Concepts
1. Collection
• A collection is a
grouping of MongoDB
documents. A collection
is the equivalent of a
table in a relational
database.
Advantages of Using
Collections:
• Flexibility: Each
document in a collection
can have its own set of
fields. Structural
changes are easier to
manage than in
traditional, rigid SQL
tables.
• Scalability: Collections
can be distributed
across multiple servers
or clusters to handle
large data volumes.
2. Document
• Synonymous with a
record, a document is
the main data storage
unit in MongoDB. It is a
set of key-value pairs.
○ Key: The field
name
○ Value: The data
Document-Key Pairs:
• Each document
maintains a unique ID,
known as the object
ID which is
autogenerated. This
ensures every document
is distinct.

Mongoose Page 53
is distinct.
• Unlike SQL databases
where each row of a
table follows the same
schema, a document can
be more fluid,
accommodating fields as
required.
Considerations When
Choosing the Level of
Normalization:
• Optimized Reads:
Normalization into
separate collections may
be beneficial if there are
large amounts of data
that might not always
have to be fetched.
• Batch Inserts and
Updates:
Denormalization often
leads to simpler write
operations. If there will
be a lot of changes or
inserts, denormalization
can be more efficient.
• Atomicity: When data
that belongs together is
split into different
collections, ensuring
atomicity can become
difficult.
3. Field
• A field is a single piece
of data within a
document. It's
synonymous with a
database column.
○ Field Type:
MongoDB
supports multiple
field types,
including arrays.
○ Limit on Nested
Fields: Documents
can be nested,
which is like being
able to have sub-
documents within
a main document.
However, there is
a depth limitation:
you can't embed
documents
endlessly.
Schema
MongoDB is often regarded
as schema-less, but a more
accurate description is that
it's flexible. While documents
within a single collection can
have different fields, a robust
schema design process is still
essential.
Adapting to Evolving Schemas:
• Versioning: Managed
schema changes and
versioning in the

Mongoose Page 54
versioning in the
application layer.
• Schema Validation:
Introduced in MongoDB
3.2, this feature allows
for the application of
structural rules to
documents.
• Education and Training:
Properly educating
developers on the use of
a database can minimize
potential misuse of its
flexibility.
• Use of Techniques to
Ensure Data Integrity:
Techniques such as
double-entry
bookkeeping can assure
data accuracy, especially
when dealing with
multiple, occasionally
outdated records.
Modeling vs. Tuning Approaches
• Normalization: Seeks to
reduce redundancy and
improve data
consistency.
• Denormalization:
Emphasizes
performance gains.
Redundancies are
knowingly introduced
for optimized and rapid
reads.
• Use Cases Dictate:
Neither is definitively
superior; their suitability
depends on the specific
use case.
7. What is the default port on
which MongoDB listens?
The default port number for
MongoDB is 27017. While it is
possible to run multiple
instances of MongoDB on the
same machine, each instance
must have its unique port
number to ensure they don't
conflict.
8. How does MongoDB provide
high availability and disaster
recovery?
MongoDB ensures high
availability and disaster
recovery through a robust
data architecture and a
distributed system model. It
integrates various mechanisms
to maintain data integrity,
uptime assurances, and data
redundancy.
Key Components
1. Replica Sets: These are
clusters of MongoDB
nodes that use
automatic failover to
maintain data

Mongoose Page 55
maintain data
consistency.
2. WiredTiger Storage
Engine: It powers
numerous features
including data durability,
in-memory storage, and
compression.
3. Oplog: Short for
"operations log", it
records all write
operations in an
append-only manner.
4. Write Concerns: These
are rules that determine
the level of
acknowledgment
required for write
operations.
5. Read Preferences: They
define which nodes in a
cluster can satisfy read
operations.
6. Data Centers: Hardware
resilience can be
achieved by distributing
nodes across multiple
data centers.
7. Backups and Restores:
MongoDB offers built-in
mechanisms to backup
and restore data, further
aiding in disaster
recovery.
8. Monitoring Tools: For
performance tracking
and potential issue
detection.
9. Technology Agnostic:
Can deploy on multi-
cloud, hybrid and on-
premises architectures.
Data Recovery Modes
1. Restore: Achieved
through the backup of
data when the config
server is the only
component that is active
and accurate. This
method doesn't
consider data changes
made after the backup
was captured.
2. Oplog Replays: This
involves using oplogs
that track changes,
ensuring that even after
a cluster restart, any
missed transactions are
reinstated.
3. Snapshotting: It is a
consistent snapshot of
data across the nodes in
the replica set.
Code Example: Write Concerns
and Oplog
Here is the Python code:
# Import the MongoClient

Mongoose Page 56
# Import the MongoClient
class from pymongo.
from pymongo import
MongoClient
# Establish connection to the
MongoDB server using
MongoClient.
client =
MongoClient('mongodb://local
host:27017/')
# Assign the test database to
a variable
db = client.test
# Assign the collection within
the test database to a variable
collection = db.test_collection
# Insert a document into the
collection and set the write
concern to 'majority'
result =
collection.insert_one({'test_ke
y': 'test_value'},
write_concern={'w': 'majority'})
# Fetch the oplog entry
associated with the insert
operation.
oplog_cursor =
db.local.oplog.rs.find({'ns':
'test.test_collection', 'op': 'i'})
# Access the result and
compare the count to ensure
the operation was recorded in
the oplog.
operation_count =
oplog_cursor.count()
Recommendations
• Employ consistent and
comprehensive backup s
trategies in conjunction
with multi-faceted
recovery plans.
9. What
are indexes in MongoDB, and
why are they used?
Indexes are employed
in MongoDB to optimize
database queries by providing
faster access to data. Without
indexes, MongoDB performs
full collection scans.
Common Types of Indexes in
MongoDB
• Single Field Index: The
most basic form of
index.
• Compound Index:
Generated across
multiple fields; used for
queries involving these
fields.
• Multikey Index:
Specially designed for
arrays or embedded
documents.
Batch Insert Operations on an
Indexed Collection Describe
any performance bottlenecks
you anticipate.
• Text Index: Suited for
text searches, often
leveraging stemming
and stop words.
Unique Explain in which

Mongoose Page 57
Unique Explain in which
situations it's beneficial to
manage a unique index.
Discard icon GEO Index
Describe the purpose of this
index type and the type of
queries it can optimize.
• TTL (Time-to-Live)
Index: Deletes
documents after a
specified duration,
suitable for logs and
cached data.
Common Performance
Bottlenecks with Indexes
• Index Overuse: Too
many indexes can
degrade write
performance.
• Index Size: Larger
indexes consume more
RAM and might slow
down read and write
operations.
• Index Inefficiency:
Inaccurate or non-
selective index usage
can render them
ineffective.
• Write Penalties: Indexes
incur an overhead
during writes, impacting
their efficiency in write-
heavy systems.
• Index Maintenance:
Regular maintenance,
like rebuilding or
reorganizing indexes, is
often necessary.
• Workload
Misalignment: An index
might not be beneficial
if it's not aligned with
the actual query
workload.
Make sure to keep the indexes
required and remove any
unnecessary ones.
10. What is the role of the id
field in MongoDB documents?
The _id Field in MongoDB
serves as a primary key and
provides several key
functionalities:
• Uniqueness Guarantee:
Each document must
have a unique _id, which
ensures data integrity.
• Automatic Indexing:
Automated indexing
based on _id enhances
query efficiency.
• Inherent Timestamp:
The _id can have an
embedded timestamp,
useful for time-based
operations.
For instance, with

Mongoose Page 58
For instance, with
an ObjectId, the first 8
characters represent a 4
byte timestamp:
timestamp=substr(Objec
tId,0,8)
• Concurrency Control: If
multiple write
operations with the
same _id occur
simultaneously,
MongoDB uses a
technique called last-
write wins to manage
the conflict:
The document with the
most recent _id value, or
timestamp if using an
ObjectId, supersedes the
others.
• Modify and Return:
When executing an
operation to insert a
new document or find &
modify an existing one,
you can request to
return the modified
document and its _id.
ObjectId vs. Custom _id
While MongoDB
provides automatic
ObjectId generation,
documents can also use
custom values.
• Custom
Representations:
Unleash flexibility by
using custom strings,
numbers, or other valid
BSON types for
the _id field.
• Controlled Uniformity:
Design your
own _id strategy to align
with data, such as
employing natural keys
for documents
originating from specific,
external sources.
• Migrate with Care: Once
an application is live,
altering the structure
can be intricate.
Transition plans are vital
for a seamless shift.
• Custom Indexing:
Managing an index on a
uniquely generated
custom _id turns the
data into a compact,
high-throughput
structure.
Schema Design and the _id Field
The choice between automatic
ObjectId and
custom _id values links back to
the intended data model,
data access patterns, and

Mongoose Page 59
data access patterns, and
specific domain requirements.
While using the automatic
ObjectId brings about benefits
like ease of
use and embedded
timestamp,
custom _id generation
provides finer control and
helps in scenarios where a
specific data structure is
favored or where external
data sources need to be
integrated.
11. How do you create a
new MongoDB collection?
The process for creating a new
collection in MongoDB is
simple and instantaneous.
Benefits of Instantaneous
Creation
• MongoDB collections
are schemaless, leading
to immediate collection
creation.
• Document structure and
content drive schema
design.
• No predefined schema
requirements allow for
dynamic, evolving data
models.
Steps to Create a Collection
1. Select the
Database: Ensure you
are connected to the
intended database for
the collection's creation.
Switch to the desired
database using use in
the mongo shell or
select the database
programmatically in
your driver's API.
2. Perform a Write
Operation: The new
collection is created the
moment you execute a
write operation such
as insert, update,
or save.
3. Check Collection
Existence
(Optional): While not
necessary for the
creation process, you
can verify the collection
is created using the
listCollections method.
12. What is the syntax to insert
a document into a MongoDB
collection?
To insert a document into
a MongoDB collection, you
can use
the insertOne() method,
which accepts the document
as an argument:

Mongoose Page 60
as an argument:
db.collectionName.insertOne({
key1: "value1",
key2: 2,
key3: [1, 2, 3],
key4: { nestedKey:
"nestedValue" }
});
Alternatively, you can use
the insertOne() method,
supply an array of documents
with insertMany():
db.collectionName.insertMany
([
{ key: "value1" },
{ key: "value2" }
]);
13. Describe how to read data
from a MongoDB collection.
To read data from a MongoDB
collection, you use
the find method with various
options for querying and data
manipulation.
Key Methods
• find(filter, projection):
Retrieves documents
based on filter
conditions. You can
specify which fields to
include or exclude in the
result (projection).
• findOne(filter,
projection): Similar
to find but retrieves only
the first matching
document.
• distinct(field, filter):
Returns a list of distinct
values for a specific
field, optionally filtered.
Query Operators
• Comparison: $eq, $gt, $l
t, $in, $nin, etc.
• Logical: $and, $or, $not,
$nor, etc.
• Element: $exists, $type
• Evaluation: $regex, $mo
d, $text
• Geospatial: $geoNear, $
geoWithin, etc.
Aggregation
MongoDB also provides
the aggregation
framework for complex
operations, using a pipeline of
various stages
like match, group, sort, limit,
etc.
Example: Basic Find Query
Here is a Python code:
import pymongo
client =
pymongo.MongoClient("mong
odb://localhost:27017/")
db = client["mydatabase"]
collection = db["mycollection"]
# Retrieve all documents
all_documents =
collection.find()
# Alternatively, you can iterate

Mongoose Page 61
# Alternatively, you can iterate
through the cursor:
for doc in all_documents:
print(doc)
Example: Querying with Filters
Here is a Python code:
# Let's say we have the
following documents in the
collection:
# [{
# "name": "John",
# "age": 30,
# "country": "USA"
# },
# {
# "name": "Jane",
# "age": 25,
# "country": "Canada"
# }]
# Retrieve documents where
the name is "John"
john_doc =
collection.find_one({"name":
"John"})
print(john_doc) # Output:
{"name": "John", "age": 30,
"country": "USA"}
# Retrieve documents where
age is greater than or equal to
25 and from country "USA"
filter_criteria = {"age": {"$gte":
25}, "country": "USA"}
docs_matching_criteria =
collection.find(filter_criteria)
for doc in
docs_matching_criteria:
print(doc)
# Output: {"name": "John",
"age": 30, "country": "USA"}
Projection
Projection helps control the
fields returned. It uses a
dictionary where fields to
include are marked with 1,
and those to exclude with 0.
For instance, {"name": 1,
"age": 1, "_id": 0} only
includes name and age while
excluding _id:
Here is a Python code:
# Retrieve the name and age
fields, ignoring the _id field
docs_with_limited_fields =
collection.find({}, {"name": 1,
"age": 1, "_id": 0})
for doc in
docs_with_limited_fields:
print(doc)
# Output: {"name": "John",
"age": 30}
# {"name": "Jane",
"age": 25}
Sort, Skip, and Limit
sort, skip, and limit help in
reordering, pagination, and
limiting the result size.
Here is a Python code:
# Sort all documents by age in
descending order
documents_sorted_by_age =
collection.find().sort("age", -1)
# Skip the first two documents
and retrieve the rest
documents_after_skipping =
collection.find().skip(2)
# Limit the number of
documents returned to 3

Mongoose Page 62
documents returned to 3
limited_documents =
collection.find().limit(3)
Distinct Values
Here is a Python code:
# Suppose, the collection has
a "country" field for each
document
# Get a list of distinct
countries
distinct_countries =
collection.distinct("country")
print(distinct_countries) #
Output: ["USA", "Canada"]
Indexes
Indexes improve read
performance. Ensure to use
appropriate indexes for
frequent and complex queries
to speed up data retrieval. If
the queries differ from the
indexing pattern or if the
collection is small, the gain
from indexing might be
insignificant, or it could even
affect the write performance
of the database. Choose an
indexing strategy based on
your data and usage patterns.
For example, if you frequently
query documents based on
their "country" field, consider
creating an index on that field:
Here is a Python, PyMongo
code:
collection.create_index("count
ry")
This would make lookups
based on the "country" field
more efficient.
14. Explain how to
update documents in MongoDB.
MongoDB offers several ways
to update documents
(equivalent to SQL's "rows").
Let’s look at the most common
methods.
Update Methods
• Replace: Entire
document is updated.
This is the closest
equivalence to
SQL's UPDATE stateme
nt.
• Update: For selective
field updates, you
use $set, $inc, $push, $u
nset, and more. This
resembles
SQL's UPDATE with
selective column
updates.
Replace & Update in MongoDB
Top-Down Approach Using
Replace
• Method: db.collectionN
ame.updateOne()
• Code:
db.collectionName.upda
teOne(
{"name": "John Doe"},

Mongoose Page 63
{"name": "John Doe"},
{$set: {"age": 30}}
);
• Use-Case: When
replacing an entire
document isn't needed.
For example, when
changing a user's email
address.
Bottom-Up Approach Using
Update + $set
• Method: db.collectionN
ame.replaceOne()
• Code:
db.collectionName.repla
ceOne(
{"name": "John Doe"},
{"name": "John Doe",
"age": 30}
);
• Use-Case: When an
entire document needs
updating or replacing,
such as a product detail
or a user’s information.
15. What are the MongoDB
commands for
deleting documents?
MongoDB offers several
methods for deleting
documents.
Deletion Methods in MongoDB
1. deleteOne(): Deletes the
first matched document.
2. deleteMany(): Removes
all matching documents.
3. remove(): Legacy
function;
use deleteOne() or delet
eMany() instead.
General Syntax
• For deleteOne(), the
syntax is:
○ db.collection.dele
teOne({filter},
{options})
• For deleteMany(), the
syntax is:
○ db.collection.dele
teMany({filter},
{options})
Code Example: Deleting One or
Many
Here is the MongoDB shell
script:
// Connect to the database
use myDB;
// Delete a single document
from 'myCollection'
db.myCollection.deleteOne({ n
ame: "Document1" });
// Delete all documents from
'myCollection' with the
condition 'age' greater than 25
db.myCollection.deleteMany({
age: { $gt: 25 } });

Mongoose Page 64
MongoDB Interview Questions
1) What do you understand by NoSQL databases? Is MongoDB a NoSQL database? explain.
At the present time, the internet is loaded with big data, big users, big complexity etc. and also
becoming more complex day by day. NoSQL is answer of all these problems, It is not a
traditional database management system, not even a relational database management system
(RDBMS). NoSQL stands for "Not Only SQL". NoSQL is a type of database that can handle and
sort all type of unstructured, messy and complicated data. It is just a new way to think about the
database.
Yes. MongoDB is a NoSQL database.

2) Which are the different languages supported by MongoDB?


MonggoDB provides official driver support for C, C++, C#, Java, Node.js, Perl, PHP, Python,
Ruby, Scala, Go and Erlang.
You can use MongoDB with any of the above languages. There are some other community
supported drivers too but the above mentioned ones are officially provided by MongoDB.

3) What are the different types of NoSQL databases? Give some example.
NoSQL database can be classified as 4 basic types:
1. Key value store NoSQL database
2. Document store NoSQL database
3. Column store NoSQL database
4. Graph base NoSQL databse
There are many NoSQL databases. MongoDB, Cassandra, CouchBD, Hypertable, Redis, Riak,
Neo4j, HBASE, Couchbase, MemcacheDB, Voldemort, RevenDB etc. are the examples of
NoSQL databases.

4) Is MongoDB better than other SQL databases? If yes then how?


MongoDB is better than other SQL databases because it allows a highly flexible and scalable
document structure.
For example:
• One data document in MongoDB can have five columns and the other one in the same
collection can have ten columns.
• MongoDB database are faster than SQL databases due to efficient indexing and storage
techniques.

5) What type of DBMS is MongoDB?


MongoDB is a document oriented DBMS

6) What is the difference between MongoDB and MySQL?


Although MongoDB and MySQL both are free and open source databases, there is a lot of
difference between them in the term of data representation, relationship, transaction, querying

Mongoose Page 65
difference between them in the term of data representation, relationship, transaction, querying
data, schema design and definition, performance speed, normalization and many more. To
compare MySQL with MongoDB is like a comparison between Relational and Non-relational
databases.

7) Why MongoDB is known as best NoSQL database?


MongoDb is the best NoSQL database because, it is:
Document Oriented
Rich Query language
High Performance
Highly Available
Easily Scalable

8) Does MongoDB support primary-key, foreign-key relationship?


No. By Default, MongoDB doesn't support primary key-foreign key relationship.

9) Can you achieve primary key - foreign key relationships in MongoDB?


We can achieve primary key-foreign key relationship by embedding one document inside
another. For example: An address document can be embedded inside customer document.

10) Does MongoDB need a lot of RAM?


No. There is no need a lot of RAM to run MongoDB. It can be run even on a small amount of
RAM because it dynamically allocates and de-allocates RAM according to the requirement of
the processes.

11) Explain the structure of ObjectID in MongoDB.


ObjectID is a 12-byte BSON type. These are:
• 4 bytes value representing seconds
• 3 byte machine identifier
• 2 byte process id
• 3 byte counter

12) Is it true that MongoDB uses BSON to represent document structure?


Yes.

13) What are Indexes in MongoDB?


In MondoDB, Indexes are used to execute query efficiently. Without indexes, MongoDB must
perform a collection scan, i.e. scan every document in a collection, to select those documents
that match the query statement. If an appropriate index exists for a query, MongoDB can use
the index to limit the number of documents it must inspect.

14) By default, which index is created by MongoDB for every collection?


By default, the_id collection is created for every collection by MongoDB.

15) What is a Namespace in MongoDB?


Namespace is a concatenation of the database name and the collection name. Collection, in
which MongoDB stores BSON objects.

16) Can journaling features be used to perform safe hot backups?


Yes.

17) Why does Profiler use in MongoDB?


MongoDB uses a database profiler to perform characteristics of each operation against the
database. You can use a profiler to find queries and write operations

18) If you remove an object attribute, is it deleted from the database?


Yes, it be. Remove the attribute and then re-save() the object.

19) In which language MongoDB is written?


MongoDB is written and implemented in C++.

20) Does MongoDB need a lot space of Random Access Memory (RAM)?
No. MongoDB can be run on small free space of RAM.

21) What language you can use with MongoDB?


MongoDB client drivers supports all the popular programming languages so there is no issue of
language, you can use any language that you want.

22) Does MongoDB database have tables for storing records?

Mongoose Page 66
22) Does MongoDB database have tables for storing records?
No. Instead of tables, MongoDB uses "Collections" to store data.

23) Do the MongoDB databases have schema?


Yes. MongoDB databases have dynamic schema. There is no need to define the structure to
create collections.

24) What is the method to configure the cache size in MongoDB?


MongoDB's cache is not configurable. Actually MongoDb uses all the free spaces on the system
automatically by way of memory mapped files.

25) How to do Transaction/locking in MongoDB?


MongoDB doesn't use traditional locking or complex transaction with Rollback. MongoDB is
designed to be light weighted, fast and predictable to its performance. It keeps transaction
support simple to enhance performance.

26) Why 32 bit version of MongoDB are not preferred ?


Because MongoDB uses memory mapped files so when you run a 32-bit build of MongoDB, the
total storage size of server is 2 GB. But when you run a 64-bit build of MongoDB, this provides
virtually unlimited storage size. So 64-bit is preferred over 32-bit.

27) Is it possible to remove old files in the moveChunk directory?


Yes, These files can be deleted once the operations are done because these files are made as
backups during normal shard balancing operation. This is a manual cleanup process and
necessary to free up space.

28) What will have to do if a shard is down or slow and you do a query?
If a shard is down and you even do query then your query will be returned with an error unless
you set a partial query option. But if a shard is slow them Mongos will wait for them till response.

29)Explain the covered query in MongoDB.


A query is called covered query if satisfies the following two conditions:
• The fields used in the query are part of an index used in the query.
• The fields returned in the results are in the same index.

30) What is the importance of covered query?


Covered query makes the execution of the query faster because indexes are stored in RAM or
sequentially located on disk. It makes the execution of the query faster.
Covered query makes the fields are covered in the index itself, MongoDB can match the query
condition as well as return the result fields using the same index without looking inside the
documents.

31) What is sharding in MongoDB?


In MongoDB, Sharding is a procedure of storing data records across multiple machines. It is a
MongoDB approach to meet the demands of data growth. It creates horizontal partition of data
in a database or search engine. Each partition is referred as shard or database shard.

32) What is replica set in MongoDB?


A replica can be specified as a group of mongo instances that host the same data set. In a
replica set, one node is primary, and another is secondary. All data is replicated from primary to
secondary nodes.

33) What is primary and secondary replica set in MongoDB?


In MongoDB, primary nodes are the node that can accept write. These are also known as
master nodes. The replication in MongoDB is single master so, only one node can accept write
operations at a time.
Secondary nodes are known as slave nodes. These are read only nodes that replicate from the
primary.

34) By default, which replica sets are used to write data?


By default, MongoDB writes data only to the primary replica set.

35) What is CRUD in MongoDB?


MongoDB supports following CRUD operations:
• Create
• Read
• Update

Mongoose Page 67
• Delete

36) In which format MongoDB represents document structure?


MongoDB uses BSON to represent document structures.

37) What will happen when you remove a document from database in MongoDB? Does
MongoDB remove it from disk?
Yes. If you remove a document from database, MongoDB will remove it from disk too.

38) Why are MongoDB data files large in size?


MongoDB doesn't follow file system fragmentation and pre allocates data files to reserve space
while setting up the server. That's why MongoDB data files are large in size.

39) What is a storage engine in MongoDB?


A storage engine is the part of a database that is used to manage how data is stored on disk.
For example: one storage engine might offer better performance for read-heavy workloads, and
another might support a higher-throughput for write operations.

40) Which are the storage engines used by MongoDB?


MMAPv1 and WiredTiger are two storage engine used by MongoDB.

41) What is the usage of profiler in MongoDB?


A database profiler is used to collect data about MongoDB write operations, cursors, database
commands on a running mongod instance. You can enable profiling on a per-database or per-
instance basis.
The database profiler writes all the data it collects to the system. profile collection, which is a
capped collection.

42) Is it possible to configure the cache size for MMAPv1 in MongoDB?


No. it is not possible to configure the cache size for MMAPv1 because MMAPv1 does not allow
configuring the cache size.

43) How to configure the cache size for WiredTiger in MongoDB?


For the WiredTiger storage engine, you can specify the maximum size of the cache that
WiredTiger will use for all data. This can be done using
storage.wiredTiger.engineConfig.cacheSizeGB option.

44) How does MongoDB provide concurrency?


MongoDB uses reader-writer locks for concurrency. Reader-writer locks allow concurrent
readers shared access to a resource, such as a database or collection, but give exclusive
access to a single write operation.

45) What is the difference between MongoDB and Redis database?


Difference between MongoDB and Redis:
• Redis is faster than MongoDB.
• Redis has a key-value storage whereas MongoDB has a document type storage.
• Redis is hard to code but MongoDB is easy.
For more information: click here

46) What is the difference between MongoDB and CouchDB?


Difference between MongoDB and CouchDB:
• MongoDB is faster than CouchDB while CouchDB is safer than MongoDB.
• Triggers are not available in MongoDB while triggers are available in CouchDB.
• MongoDB serializes JSON data to BSON while CouchDB doesn't store data in JSON
format.
For more information: click here

47) What is the difference between MongoDB and Cassandra?


Difference between MongoDB and Cassandra:
• MongoDB is cross-platform document-oriented database system while Cassandra is high
performance distributed database system.
• MongoDB is written in C++ while Cassandra is written in Java.
• MongoDB is easy to administer in the case of failure while Cassandra provides high
availability with no single point of failure.

Mongoose Page 68
availability with no single point of failure.
For more information: click here

48) Is there any need to create database command in MongoDB?


You don't need to create a database manually in MongoDB because it creates automaically
when you save the value into the defined collection at first time.

49. If while querying we get 1000's of data , how to fetch only a few?

Mongoose Page 69
Mongoose Page 70

You might also like