Questions
Questions
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.
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.
Mongoose Page 8
Sharded Client Connection
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.
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.
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.
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
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.
At Present, MongoDB
offers driver support to
C++, Java, PHP,
Perl, Python, Go, Scala,
and Ruby.
Mongoose Page 25
backups in MongoDB.
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.
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.
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.
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.
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:
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>})
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.
:~$mongo
MongoDB shell version:1.65
Connecting to: test
Error: Could not connect to the
server
Exception: connect failed
1. cd/var1/lib1/MongoDB
2. We remove the mongod.
lock from the folder
3. Sudo start MongoDB
4. Mongo
db.COLLECTION_NAME.find().limit
(NUMBER).skip(NUMBER)
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();
>
db.COLLECTION_NAME.find().limit
(NUMBER)
>
db.COLLECTION_NAME.find().sort
({KEY:1})
• Key-Value
• Graph
• Column Oriented
• Document Oriented
Mongoose Page 31
do we use for dropping
a database?
We use the “DB.drop
database” command for
dropping a database.
44) How do we
remove a document
from the collection?
By using the remove()
method, we remove a
document from the
collection.
• Client machine ID
• Client process ID
• Byte incremented counter
• Timestamp
Mongoose Page 32
• Timestamp
{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" },
}
}
)
• Query routers
• Shards
• Config servers
• Hbase
• CouchDB
• Cassandra
• Redis
• Riak
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
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})
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
}
}
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.
• AVG
• Sum
• Min
• Max
• First
• Push
• addTo Set
• Last
Create-db.collection.insert();
Read-db.collection.find();
Update-db.collection.update();
Delete-db.collection.remove();
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.
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.
69) Differences
between MongoDB
and RDBMS
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.
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
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
db.example.find( { numbers:
{ $elemMatch: { $gt: -10, $lt:
10 } } } );
db.eval(function() {
return
db.scratch.find().toArray().sort(fu
nction(doc1, doc2) {
return doc1.a – doc2.a
})
});
Mongoose Page 41
sort.
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();
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.
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.
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.
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.
20) Does MongoDB need a lot space of Random Access Memory (RAM)?
No. MongoDB can be run on small free space of RAM.
Mongoose Page 66
22) Does MongoDB database have tables for storing records?
No. Instead of tables, MongoDB uses "Collections" to store data.
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.
Mongoose Page 67
• Delete
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.
Mongoose Page 68
availability with no single point of failure.
For more information: click here
49. If while querying we get 1000's of data , how to fetch only a few?
Mongoose Page 69
Mongoose Page 70