Commonly Asked MongoDB Interview Questions (2023) - Interviewbit
Commonly Asked MongoDB Interview Questions (2023) - Interviewbit
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 ?
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.
$ mongod
$ mongo
MongoDB shell version: 4.2.0
connecting to: test
>
> x = 100;
200
> x / 5;
20
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.
The basic method for adding data to MongoDB is “inserts”. To insert a single
document, use the collection’s insertOne method:
Once a document is stored in the database, it can be changed using one of several
update methods: updateOne, updateMany, 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 expects a document with which it will replace the document
matching the filter.
{
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"name" : "alice",
"friends" : 24,
"enemies" : 2
}
The CRUD API in MongoDB provides deleteOne and deleteMany 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 documents.
For example:
> db.books.deleteOne({"_id" : 3})
Example:
> db.users.find({"age" : 24})
:
12. What are the data types in MongoDB?
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 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:
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:
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("executionStats")
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]
}
}
{
"name" : "Hudson River",
"loc" : {
"type" : "LineString",
"coordinates" : [[0,1], [0,2], [1,2]]
}
:
}
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.
If the value of a field does 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("4b253b067525f35f94b60a31"),
"name" : "alice",
"age" : 23,
"sex" : "female",
"location" : "India"
}
> db.users.updateOne({"_id" :
ObjectId("4b253b067525f35f94b60a31")},
... {"$set" : {"favorite book" : "Start with Why"}})
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.
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.
:
24. 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:
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.
:
25. What are some 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
26. Conclusion
Supports Indexing
Designed to scale
Rich with Features
High Performance
Load Balancing
Supports sharding
https://www.mongodb.com/2
https://docs.mongodb.com
Recommended Tutorials:
MongoDB MCQ
1.Which of these is not a built-in role that grants permissions for database users in
MongoDB?
read
readWrite
dbOwner
write
2.Which of the following does not come under the basic shell operations on
MongoDB?
Update
Create
:
Delete
Write
Code
ID
Date
String
5.MongoDB Queries can return specific fields of documents which also include
user-defined __________ functions.
Javascript
C++
:
All of the mentioned
6. ____________ are operations that process data records and return computed
results.
ReplicaAgg
SumCalculation
Aggregations
7.The most basic pipeline stages provide __________ that operate like queries.
methods
filters
stored procedure
tables
collections
rows
Hash
Map
B-tree
compound
composite
candidate
11.With hash-based partitioning, two documents with _____ shard key values are
unlikely to be part of the same chunk.
open
close
partially close
cluster
shard
partition
Blog About Us
FAQ Contact Us
Interview Preparation
View All
Top Articles
:
Highest Paying Jobs In India Exciting C Projects Ideas With Source Code
Top MCQ