0% found this document useful (0 votes)
29 views14 pages

Interview Ques

The document discusses upgrading MongoDB from version 3.6 to 5.0 directly by: 1) Copying the wiredTiger files to a new location 2) Uninstalling the older version and installing the newer version 3) Changing permissions and ownership of folders and restarting services However, direct upgrades are not recommended and data may crash, so it is better to use mongodump and mongorestore as a safer method.

Uploaded by

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

Interview Ques

The document discusses upgrading MongoDB from version 3.6 to 5.0 directly by: 1) Copying the wiredTiger files to a new location 2) Uninstalling the older version and installing the newer version 3) Changing permissions and ownership of folders and restarting services However, direct upgrades are not recommended and data may crash, so it is better to use mongodump and mongorestore as a safer method.

Uploaded by

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

1. Can we upgrade directly from 3.6 version to 5.

0 version
of mongo db in anyway?

 We can directly upgrade the mongodb 3.6 to 5.0 version by copying the wired tiger
files in to another location

 and uninstall the mongodb older version in server and install the newer version and
change the db path

 and change necessary permissions of folder

 and change the ownership to mongodb

 and restart the services and upgrade the tools to newer version.

 The tools is mongo export, import, dump, restore, mongosh.

 And change the compatibility version of mongodb to 5.0 and then restart the
services again

 But the issue is it is not dependent, due to data may crash sometimes. So it is
recommendable. We can do it only as a last resource.

2. What is wiredtiger file instead of doing mongodump and


restore can we wiredtiger file for data migration from one server
to another server?

 The mongodb data actually exists in wiredtiger file.

 We can simply copy or move to another location

 and also can move to another server by using secure protocol which is 'scp'

3. How to do data migration from one server to another server?

By using SCP and pem file we can migrate the data from one server to another.

The pem file of other server should be kept in resource server.

the coomand is sudo scp -i mongodb.pem filename/path


[email protected]:/destination_path
4. Shell scripting idology basic javascript & python?
5. How to filter the logs between particular time and
particular name?
sudo sed -n '/2023-07-04T06:45:/,/2023-07-04T06:55:/{/Slow query/p}'
/var/log/mongodb/mongod.log

6. How index created in background give me the command?


In MongoDB, you can create indexes in the background using the background
option. When an index is created in the background, it allows other operations on
the database to continue while the index is being built. This can be especially useful
for creating indexes on large collections, as it minimizes the impact on the database's
performance during the indexing process.

Here's the command to create an index in the background in MongoDB:

javascriptCopy code
db.collection.createIndex({ "field_name": 1 }, { background: true })

Let's break down the components of this command:

 db.collection.createIndex: This is the method used to create an index on a


specific collection. Replace collection with the name of your collection.
 { "field_name": 1 }: This is the field for which you want to create an index.
Replace "field_name" with the name of the field, and 1 indicates that it's an
ascending index. You can use -1 for descending indexes.
 { background: true }: This option specifies that the index should be created in the
background. The true value indicates that the index creation should not block other
operations on the collection.

By setting the background option to true, the index will be created without
interfering with other database operations. Keep in mind that index creation in the
background may take longer than creating an index in the foreground (the default
behavior), but it's often preferred for large collections or in production environments
where minimizing disruption is important.

Here's an example command to create a background index on a collection named


"myCollection" for a field called "fieldName":

javascriptCopy code
db.myCollection.createIndex({ "fieldName": 1 }, { background: true })

Remember to replace "myCollection" with the actual name of your collection and
"fieldName" with the name of the field for which you want to create the index.
7. What do we use compact in mongo db ? and create a user with
compact action
In MongoDB, the compact action is used to reclaim disk space and reduce storage
fragmentation. It can help optimize the storage of data by removing deleted data and compacting c

To compact a collection in MongoDB using the compact action, you can use the
method to run a compact command on a specific collection. Here's an example of how to use theac
runCommand
compact
Copy code
db.runCommand({
javascript
compact: 'your_collection_name', force: true
})

In this command:

Replace compact.with the name of the collection you want to


'your_collection_name'

force: true is used to forcibly perform the compaction. This is necessary because
compaction may have a negative impact on the database's performance while it's being executed. U
compact

After running the command, MongoDB will compact the specified collection by removing unused o

Keep in mind that the compact action should be used with caution in a production
environment, and it may not always be necessary. MongoDB's storage engine, such as WiredTiger, a

db.createRole(
{
role: "myCustomCompactRole",
privileges: [
{
resource: { "db" : "<database>" , "collection" : "<collection>" },
actions: [ "compact" ]
}
],
roles: []
})
8. How to create a collection with only particular /predefined fields
can be added. Other than that “10” new field can be added
Validation schema in detail and get the details

In MongoDB, you can create a collection with predefined fields by inserting


documents with those fields. Unlike traditional relational databases, MongoDB
collections are schema-less, meaning that documents within a collection can
have different fields. Fields are created dynamically as documents are inserted.

However, you can enforce a schema or structure on your collection by specifying


the fields you want to be predefined in your documents. While MongoDB doesn't
provide a mechanism to define a strict schema upfront, you can ensure that
documents adhere to your desired structure in the application code that interacts
with MongoDB.

Here's an example of how you can insert documents into a collection with
predefined fields:

1. Connect to your MongoDB server using the programming


mongo shell orlanguage
a MongoDB
of choice.
driver in your

Choose a database for your collection or create a new one using thecommand.
use
Copy code
javascript

Insert documents into your collection, specifying the predefined fields in each document. For example:
Copy code
db.my_collection.insert({ "field1": "value1", "field2": 42,
javascript
"field3": ["item1", "item2"], "field4": {
"nested_field1": "nested_value1", "nested_field2": 123
}
})
In this example, we've inserted a document with four predefined fields: field1, field2, field3, and fi

By inserting documents with predefined fields, you effectively define the structure
of your collection. However, MongoDB won't enforce this structure; it's up to your
application code to ensure that documents adhere to the predefined structure. You
can use validation rules, application-level checks, and data modeling practices to
maintain consistency within your collection.

If you need more control over schema validation, you can implement schema
validation rules starting from MongoDB 3.6 and higher. These rules allow you to
define a schema and enforce specific data types, validation expressions, and more.
Here's an example of how to enable schema validation for a collection:
javascriptCopy code
db.createCollection("my_collection", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["field1", "field2", "field3", "field4"],
properties: {
field1: { bsonType: "string" },
field2: { bsonType: "int" },
field3: { bsonType: "array" },
field4: {
bsonType: "object",
properties: {
nested_field1: { bsonType: "string" },
nested_field2: { bsonType: "int" }
}
}
}
}
}
})

This example creates a collection called "my_collection" and enforces a specific


schema with data types and required fields. Any documents that do not conform to
this schema will not be inserted into the collection. This provides more rigid schema
validation compared to the dynamic schema approach
In MongoDB, you can define a validation schema for your collections using the
$jsonSchema keyword to specify the data types, constraints, and rules for your fields.
This schema allows you to ensure that documents added to the collection adhere to
the specified structure. Below, I'll provide a detailed example of creating a collection
with a comprehensive JSON schema validation that enforces data types, required
fields, and additional constraints. We'll also explore how to retrieve validation details.

Let's consider a sample collection called "employees" with various fields and detailed
schema validation:

javascriptCopy code
// Create a collection with a JSON schema validation
db.createCollection("employees", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["firstName", "lastName", "birthDate", "hireDate",
"salary"],
properties: {
firstName: { bsonType: "string" },
lastName: { bsonType: "string" },
birthDate: { bsonType: "date" },
hireDate: { bsonType: "date" },
salary: {
bsonType: "double",
minimum: 0,
maximum: 1000000
},
email: {
bsonType: "string",
pattern: "^\\S+@\\S+\\.\\S+$",
description: "Must be a valid email address"
},
department: { bsonType:
"string" }, position: { bsonType:
"string" }, isActive: { bsonType:
"bool" }, skills: {
bsonType:
"array", items:
{
bsonType: "string"
}
}
}
}
}
})

We've created a collection named "employees" and defined a JSON schema validation using theoption.
We specified that documents in the collection must be of BSON type "object."
validator
We defined the required fields that must be present in every document: "firstName," "lastName," "birthD
We specified the data types for each field. For example, "firstName" and "lastName" must be strings, "bir
We added additional constraints, such as the "email" field, which must match the regular expression for
Fields like "department," "position," "isActive," and "skills" have defined data types but are not required.

Now, if you try to insert a document into the "employees" collection that doesn't
adhere to this schema, MongoDB will reject the insertion.

To retrieve the validation details of a collection, you can use the following command
in the MongoDB shell:

javascriptCopy code
db.getCollectionInfos({ name: "employees" })[0].options.validator

This command fetches the validation details for the "employees" collection, allowing
you to review the schema and constraints that have been defined.

By using JSON schema validation, you can ensure data consistency and enforce
structured data in your MongoDB collections.
9. Index fragmentation in mongo db checking the steps in it ?

Index fragmentation in MongoDB can occur over time as data is inserted, updated,
1. Connect to MongoDB: Use the
mon shell
or deleted from a collection. Fragmented or a MongoDB
indexes client
can lead to to connect
reduced query to your
MongoDB server.
performance and increased storage usage. To check for index fragmentation and
2. Select a Database: Switch to the database that contains the collection with the
optimize indexes in MongoDB, you can follow these steps:
potentially fragmented index. For example, use the use command:
javascriptCopy code
use your_database
3. List Indexes: Use the getIndex method to list the indexes on the collection and
review their details:
javascriptCopy code
db.your_collection.getIndexes()
Replace "your_collection" with the name of your collection. This command will
provide information about each index, including its name, key, and size.
4. Analyze Index Size: Analyze the size of each index, especially focusing on large
indexes that may be candidates for optimization. Larger indexes can be more prone
to fragmentation.
5. Run compact Command (Optional): If you have identified a fragmented index that
needs optimization, you can use the compa command to compact the collection
and its indexes. Here's how you can use
it:
javascriptCopy code
db.runCommand({ compact: "your_collection" })
Replace "your_collection" with the name of the collection you want to compact.
6. Rebuild Indexes (Optional): If the compa command doesn't effectively optimize
the fragmented index, you can consider rebuilding the index from scratch. This
involves dropping the index and recreating it. For example:
javascriptCopy code
db.your_collection.dropIndex("index_name")
db.your_collection.createIndex({ field1: 1, field2: 1 })
Replace "index_name" with the name of the index you want to rebuild, and define
the index fields in the createIndex command.
7. Monitor and Review: After running the compa or reindexing operations, closely
monitor the performance of your queries and the storage usage. Ensure that the
fragmentation issues have been resolved and that the indexes are performing
optimally.
8. Scheduled Maintenance: Consider implementing scheduled maintenance tasks to
regularly check for and optimize fragmented indexes. This can help prevent
fragmentation from affecting performance over time.

It's important to note that MongoDB, especially with modern storage engines like WiredTiger, hand
Fragmentation should only be a concern in specific situations, and the need for optimization may

Additionally, index optimization should be carefully planned and tested in a controlled environmen

when do we do index fragmentation ,what are the advantages?


Index fragmentation is a condition that can occur in a database, including MongoDB,
as data is inserted, updated, and deleted over time. Fragmentation in the context of
indexes typically refers to the disorganization or inefficiency of data stored in the
index data structures. This can lead to several issues, and addressing index
fragmentation can provide various advantages:

1. Improved Query Performance:

 Fragmented indexes can lead to slower query performance because they may
require more disk reads to locate the desired data. By optimizing indexes and
reducing fragmentation, you can speed up query processing.

2. Reduced Storage Usage:

 Fragmented indexes can occupy more storage space than necessary. By


defragmenting or optimizing indexes, you can reduce storage requirements,
which can be particularly beneficial for large databases.

3. Minimized Disk I/O:

 Fragmented indexes may lead to increased disk I/O due to more frequent disk
reads. Optimizing indexes can reduce the number of I/O operations required to
satisfy queries.

4. Better Memory Utilization:

 Fragmented indexes may lead to inefficient use of memory because the working
set may not be stored in memory as effectively. Optimized indexes help improve
the use of available memory for caching frequently accessed data.

5. Enhanced Index Maintenance:

 Fragmented indexes can lead to increased index maintenance overhead, which


may slow down insert and update operations. Optimizing indexes can reduce this
overhead and improve the efficiency of data modification operations.

6. Improved Database Efficiency:


 Reducing index fragmentation can lead to overall database efficiency
improvements, as queries and data manipulation operations become faster and
require fewer system resources.

7. Consistent Query Performance:

 Fragmented indexes can result in query performance degradation over time. By


addressing fragmentation, you can maintain more consistent and predictable
query performance as your database grows.

8. Less Frequent Index Rebuilds:

10. What are the data types which has to be prioritized 1st in the
 Without addressing fragmentation, you may find it necessary to rebuild indexes
documents give me
more frequently. an examples?
Optimizing indexes can reduce the need for full reindexing, which
can be resource-intensive.
When designing a MongoDB schema, it's important to choose appropriate data
It's important
types for your to notetothat
fields modern
optimize database
query management
performance systems,
and storage including
efficiency. While
MongoDB
the choice with
of datathetypes
WiredTiger storage
can vary engine,
depending onareyourdesigned
specific to manage
use indexare
case, there
fragmentation
certain efficiently.
data types that areThey use techniques
commonly like
prioritized forautomatic
efficiencycompaction
and accuracy.andHere
page
are reorganization
some data types tothatmitigate fragmentation.
are often prioritized andIn many cases,
examples of you
when may not them:
to use need
to manually address fragmentation unless you observe significant performance
1. ObjectId (Primary Key):
degradation.
An ObjectId is a 12-byte identifier typically used as the primary key for documents in a collection.
Example:there are situations where manual intervention is beneficial, such as when
However,
Copy code
you have very large indexes or specific workloads that generate significant
"_id": json
fragmentation. In such cases, periodically checking for and optimizing fragmented
ObjectId("64e277ea2a9d679e717131e9")
indexes can help maintain optimal database performance and resource utilization.
2. String (Text Data):
Strings are used for text data, such as names, descriptions, and identifiers.
Example:
json Copy code
"name": "John Doe"
3. Number (Numeric Data):
 Numbers are used for various numeric data types, including integers
and floating-point numbers.
 Examples:
jsonCopy code
"age": 30
"price": 19.99
4. Date (Timestamps):
 Dates are used to store timestamps and dates.
 Example:
jsonCopy code
"birthDate": ISODate("1990-01-15T00:00:00Z")
5. Boolean (True/False):
 Booleans are used for binary values representing true or false.
 Example:
jsonCopy code
"isCompleted": true
6. Array (Lists of Values):
 Arrays can store lists of values, such as tags, items, or related documents.
 Example:
jsonCopy code
"tags": ["tag1", "tag2", "tag3"]
7. Embedded Documents (Subdocuments):
 Embedded documents are used to represent structured data within a
document.
 Example:
jsonCopy code
"address": {
"street": "123 Main St",
"city": "New York",
"zipcode": "10001"
}
8. GeoJSON (Geospatial Data):
 GeoJSON data types are used for geospatial information like coordinates
and shapes.
 Example:
jsonCopy code
"location": {
"type": "Point",
"coordinates": [40.7128, -74.0060]
}
9. Binary Data (Binary Large Objects - Blobs):
 Binary data types are used for storing binary content like images, files,
or other binary data.
 Example:
jsonCopy code
"avatar": BinData(0, "base64-encoded-binary-data")

The choice of data types should be based on the nature of the data and the requirements of your ap
and query performance. Additionally, consider indexing fields that you frequently query or use for

11. Have you connected or migrated the data from mongo db to


document db in AWS?explain the steps in details, what are the
steps you follow?
Assessment: Understand your existing MongoDB database schema, data, and the
queries you use. Determine your data migration strategy, whether you want to
perform a one-time migration or set up ongoing data replication.
Prepare Amazon DocumentDB: Create an Amazon DocumentDB cluster in your
AWS environment. Ensure that your DocumentDB cluster configuration, including
security groups and access control, is set up correctly.
12. Choose a Migration Method:
a. One-time Data Migration: Use the AWS Database Migration Service
(DMS), custom scripts, or a tool like mongoexpo and mongoimpo to
perform a one-time data migration from MongoDB to Amazon
DocumentDB.
b. Replication: If you want to maintain real-time synchronization, you can set
up replication using a tool like Apache Kafka or built-in MongoDB
replication features, depending on your specific requirements.
13.Data Transformation: You may need to transform your MongoDB data to match
the structure expected by Amazon DocumentDB. This might involve adjusting
data types, flattening embedded documents, and other schema changes.
14. Migrate Data:
a. For a one-time migration, use the chosen method to transfer data. AWS
DMS is a powerful and flexible option that supports various source and
target databases.
b. For ongoing replication, set up the chosen replication solution and
monitor it to ensure data consistency.
15.Testing: After data migration, perform thorough testing to ensure that data has
been migrated accurately and that your queries work as expected.
16.Update Applications: Modify your applications to connect to Amazon
DocumentDB instead of MongoDB. Update connection strings and make any
necessary code changes to accommodate differences between the
databases.
17.Monitoring and Optimization: Continuously monitor your Amazon
DocumentDB cluster to ensure it performs optimally and adjust configurations as
needed.
18. What are the different types of stages in explain
(executionstats)explain
1. featch
2.and_sorte
d
1. COLLSCAN (Collection Scan):
 This stage indicates a full collection scan, where all documents in the
collection are examined to satisfy the query. It's typically an undesirable
stage, especially for large collections, as it can be slow.
2. IXSCAN (Index Scan):
 This stage represents the use of an index to fulfill the query criteria. It's
generally more efficient than a collection scan. The index name and
direction are often provided.
3. FETCH:
 This stage is typically seen after an IXSCAN. It's used to retrieve the actual
documents after the index scan. The documents may not be in the index, and
the FETCH stage retrieves them from the collection.
4. SORT:
 This stage is present when sorting is required to satisfy the query. It can
appear before or after other stages, depending on whether sorting is
performed before or after other operations.
5. PROJECTION:
 This stage represents the projection of specific fields from documents. It
can occur after other stages like IXSCAN or FETCH.
6. LIMIT:
 This stage indicates that
LIM operation has been applied to restrict the
a
number of documents returned by the query.
7. SKIP:
 The SK stage is used to skip a specified number of documents before
returning results. It often works in conjunction with LIMIT.
8. GEO_NEAR_2DSPHERE:
 This stage is related to geospatial queries, specifically when using the
2dsphere index for location-based queries.
9. SORT_KEY_GENERATOR:
 This stage is responsible for generating keys for sorting. It may be seen
in conjunction with a SORT stage.
10. COUNT:
 The COU stage is used to count the number of documents that match the
query criteria.
11. SHARD_MERGE:
 This stage is related to sharded clusters and represents the merging of
results from multiple shards.
19. Create the audit log based on the slow quries?

Creating an audit log for slow queries in MongoDB can be useful for tracking and
1. Enable Profiling:
analyzing the performance of your database. To implement this, you can follow
 MongoDB provides a profiling feature that records information about slow-
these steps:
running queries. You can enable profiling by setting the profiling level to a
value greater than 0. The profiling level can be set at the system level or for
a specific database.
 To set the profiling level at the system level (for all databases), use the
following command:
javascriptCopy code
db.setProfilingLevel(1) // 1 or 2 for profiling
 To set the profiling level for a specific database (e.g., "mydb"):
javascriptCopy code
db = db.getSiblingDB("mydb")
db.setProfilingLevel(1)
2. Configure Profiling Threshold:
 You can configure the threshold for what constitutes a "slow" query. This
threshold is defined in milliseconds. For example, to capture queries that
take
longer than 100 milliseconds, set the slow value.
 To set the slowms value for the system:
javascriptCopy code
db.setProfilingLevel(1, { slowms: 100 })
3. View the Profiling Data:
 Slow query data is stored in the system.profile collection. You can query this
collection to view the profiling data.
javascriptCopy code
db.system.profile.find({ millis: { $gt: 100 } }).sort({ ts: -1 })
 This query retrieves slow queries that took longer than 100 milliseconds and
sorts them by the timestamp in descending order.
4. Log to a File or External System:
 To create an audit log, you can redirect the output to a file or send it to an
external logging system.
 For example, you can use mongoexpo utility to export the profiling data
the to a JSON file:
shCopy code
mongoexport --db yourdb --collection system.profile --query '{
millis: { $gt: 100 } }' --out slow_queries.json
5. Analyze and Alert:
 You can use scripting or log parsing tools to analyze the exported data and
create alerts based on certain criteria, such as the number of slow queries
or specific query patterns.
 Tools like Elasticsearch and Kibana can be used to store and visualize
profiling data and set up alerts.

6. Rotate Log Files (Optional):
 If you are logging to files, consider implementing a log rotation strategy to
manage the size and retention of log files. This can be done using utilities
like
logrotate on Linux systems.

Remember to periodically review and analyze the audit log to identify


performance bottlenecks and optimize your queries and indexes accordingly.
Proper query optimization can help reduce the occurrence of slow queries in your
MongoDB deployment.

You might also like