MongoDB Viva Questions1
MongoDB Viva Questions1
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
Fixed, predefined
Schema Schema-less, dynamic schema
schema
Slower compared to
Faster, particularly for handling
Performance MongoDB for large
large-scale data
datasets