Interview Ques
Interview Ques
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 restart the services and upgrade the tools to newer version.
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.
and also can move to another server by using secure protocol which is 'scp'
By using SCP and pem file we can migrate the data from one server to another.
javascriptCopy code
db.collection.createIndex({ "field_name": 1 }, { background: true })
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.
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:
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
Here's an example of how you can insert documents into a collection with
predefined fields:
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" }
}
}
}
}
}
})
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
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.
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.
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.
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
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.