0% found this document useful (0 votes)
6 views6 pages

MongoDB Viva Questions1

The document provides an overview of MongoDB, highlighting its NoSQL nature, document-oriented data model, and differences from traditional SQL databases. It covers key concepts such as collections, databases, CRUD operations, indexing, and aggregation pipelines, along with advantages like flexibility, scalability, and high availability. Additionally, it outlines various MongoDB syntax for operations and comparisons with relational databases.

Uploaded by

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

MongoDB Viva Questions1

The document provides an overview of MongoDB, highlighting its NoSQL nature, document-oriented data model, and differences from traditional SQL databases. It covers key concepts such as collections, databases, CRUD operations, indexing, and aggregation pipelines, along with advantages like flexibility, scalability, and high availability. Additionally, it outlines various MongoDB syntax for operations and comparisons with relational databases.

Uploaded by

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

MongoDB Viva Questions

1. What is MongoDB, and How Does It Differ from Traditional SQL Databases?
• MongoDB is a NoSQL database which means it does not use the traditional
table-based relational database structure. Instead of it uses a flexible and
document-oriented data model that stores data in BSON (Binary JSON)
format.
• Unlike SQL databases that use rows and columns, MongoDB stores data
as JSON-like documents, making it easier to handle unstructured data and
providing greater flexibility in terms of schema design.
2. What are Collections And Databases In MongoDB?
• Database: A container for collections, equivalent to a database in SQL.
• Collection: A group of documents, similar to tables in SQL, but schema-less.
3. Describe the Structure of a MongoDB Document.
A MongoDB document is a set of key-value pairs similar to a JSON object. Each key
is a string and the value can be a variety of data types including strings, numbers,
arrays, nested documents and more.
4. What are the Advantages of Using MongoDB Over Other Databases?
• Flexibility: MongoDB's document-oriented model allows for dynamic
schemas.
• Scalability: Built-in sharding enables horizontal scaling.
• High Availability: Replica sets provide redundancy and automatic failover.
• Performance: Efficient storage and retrieval with BSON format.
• Ease of Use: JSON-like documents make it easier for developers to interact
with data.
5. Explain the Basic Syntax of MongoDB CRUD Operations.
CRUD operations in MongoDB are used to create, read, update, and delete
documents.
• Create: db.collection.insertOne({ name: "Alice", age: 25 })
• Read: db.collection.find({ name: "Alice" })
• Update: db.collection.updateOne({ name: "Alice" }, { $set: { age: 26 } })
• Delete: db.collection.deleteOne({ name: "Alice" })
6. What is a primary key in MongoDB?
In MongoDB, the `_id` field serves as the primary key for a document. It must be
unique within a collection and is automatically generated if not provided during
document insertion.
7. How do you insert data into a MongoDB collection?
You can insert data into a MongoDB collection using the `insertOne()` or
`insertMany()` method. You provide a document or an array of documents to be
inserted.
8. How do you update documents in MongoDB?
You can update documents in MongoDB using methods like `updateOne()`,
`updateMany(),` or `findOneAndUpdate().` You specify the query to select the
documents to update and provide an update operation.
9. What is the role of `_id` in MongoDB documents?
The `_id` field uniquely identifies each document in a collection. MongoDB uses it as
the primary key, and if not provided during document insertion, MongoDB generates
a unique value for it.
10. How do you delete data from a MongoDB collection?
You can delete data from a MongoDB collection using methods like `deleteOne()`,
`deleteMany()`, or `findOneAndDelete()`. You specify a query to select the
documents to delete.
11. What is the role of collections in MongoDB?
Collections in MongoDB are containers for organizing and storing related documents.
They act as the equivalent of tables in relational databases, grouping similar data.
12. How do you perform a query in MongoDB?
You can perform queries in MongoDB using the `find()` method, where you specify
criteria to filter documents. You can also use various query operators to refine your
queries.
13. What are the main features of MongoDB?
Some prominent features of MongoDB include flexibility in data modeling, horizontal
scalability, support for unstructured data, powerful query language, automatic
sharding, high availability with replica sets, and geospatial capabilities.
14.What is an Index in MongoDB, and How to Create One?
An index in MongoDB is a data structure that improves the speed of data retrieval
operations on a collection. You can create an index using the createIndex method.
15.How to select certain fields and ignore some fields in result?
1 - indicates that these fields will be included in the result.
0 - indicates that the _id field will be excluded from the result.
16.Explain How $, $elematch and $slice would be used in the MongoDB.
The $ operator is used to project the first matching element from an array of
embedded documents.
The $elemMatch operator is used to project the first matching element from an
array based on specified criteria.
The $slice operator is used to include a subset of the array field.
17.Difference Between RDBMS and MongoDB

Feature RDBMS MongoDB

Type of Non-relational, document-


Relational database
Database oriented database

Stores data in tables Stores data in flexible JSON-like


Data Storage
with rows and columns documents (BSON)

Fixed, predefined
Schema Schema-less, dynamic schema
schema

Joins Supports complex joins Does not support complex joins

Data Format Row-based storage Document-based storage

Slower compared to
Faster, particularly for handling
Performance MongoDB for large
large-scale data
datasets

Highly flexible, suitable for agile


Less flexible for changes
Flexibility development and evolving data
in data structure
models
Feature RDBMS MongoDB

Backup and Backup and recovery Automated backup and recovery


Recovery options are manual options available

18.Syntax for where Clause(find method)


db.collection.find({ field: value })
19. Syntax for insert record
db.collection.insert({ field: value })
20.Syntax for And operation
db.collection.find({ $and: [ { field1: value1 }, { field2: value2 } ] })
21.Syntax for OR operation
db.collection.find({ $or: [ { field1: value1 }, { field2: value2 } ] })
22. Syntax for Update operation
db.collection.updateOne( { field: value }, { $set: { fieldToUpdate: newValue } })
23.List the Comparison and Logical Operators
Comparison Selectors:
Comparison selectors are used to compare fields against specific values or other
fields. Here are some common comparison selectors:
$eq - Matches values that are equal to a specified value.
$ne - Matches all values that are not equal to a specified value.
$gt - Matches values that are greater than a specified value.
$gte - Matches values that are greater than or equal to a specified value.
$lt - Matches values that are less than a specified value.
$lte - Matches values that are less than or equal to a specified value.
$in - Matches any of the values specified in an array.
$nin - Matches none of the values specified in an array.
Logical Selectors:
Logical selectors are used to combine multiple conditions in a query. Here aresome
common logical selectors:
$and - Joins query clauses with a logical AND and requires that all conditions be true.
$or - Joins query clauses with a logical OR and requires that at least one condition
be true.
$not - Inverts the effect of a query expression and returns documents that do not
match the queryexpression.
$nor - Joins query clauses with a logical NOR and requires that none of the conditions
be true.
24.Why you create Index in MongoDB?
By creating index in MongoDB, It improves the query performance.
25.What is Unique Index?
A unique index ensures that the indexed field does not contain duplicate values.
26.What is Sparse Index?
A sparse index only includes documents that contain the indexed field, ignoring
documents where the field is missing.
27.What is Compound Index?
A compound index indexes multiple fields within a single index
28.What is Multikey Index?
A multikey index is created on an array field, indexing each element of the array.
29.Explain Aggregation pipeline?
The aggregation pipeline in MongoDB provides a flexible and powerful framework
for data processing. By chaining multiple stages, you can filter, group, sort, project,
and transform your data to suit your specific requirements.

You might also like