Skip to content

Latest commit

 

History

History
63 lines (44 loc) · 3.41 KB

File metadata and controls

63 lines (44 loc) · 3.41 KB

MongoDB Questions (Basics)

Q1. What is MongoDB?

MongoDB is a NoSQL database that uses a document-oriented data model. It stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time.

Q2. How does MongoDB differ from traditional relational databases?

  • Data Model: Uses documents and collections instead of tables and rows.
  • Schema: Schema-less, allowing dynamic changes to the structure of documents.
  • Scalability: Designed for horizontal scaling through sharding.
  • Query Language: Uses a rich, JSON-based query language.

MongoDB is schema-less, meaning you don't need to define a rigid structure for your documents upfront. You can add or remove fields as needed.

Q3. What is a document in MongoDB?

A document is a set of key-value pairs, similar to JSON objects. Each document is a record in a collection and can have different fields and structures compared to other documents in the same collection.

Q4. What is a collection in MongoDB?

A collection is a grouping of MongoDB documents. Collections are similar to tables in relational databases but without a fixed schema. Documents within a collection can have different fields.

Q5. What is the role of the _id (ObjectID) field in MongoDB?

The _id field is a unique identifier for each document in a MongoDB collection. It ensures that each document can be uniquely identified and retrieved. By default, MongoDB creates an ObjectId as the value for the _id field if it's not provided by the user.

Q6. How do you insert a document into a MongoDB collection? Provide an example.

You can insert a document into a MongoDB collection using the insertOne or insertMany methods.

db.collectionName.insertOne({
  name: "John Doe",
  age: 30,
  occupation: "Software Developer"
});

Q7. What are the different types of indexes in MongoDB?

Index Type Description Code Example
Single-field index Creates an index on a single field of a document. Useful for sorting and querying based on that field. db.collection.createIndex({ fieldName: 1 })
Compound index Creates an index on multiple fields of a document. Improves query performance for complex queries involving multiple fields. db.collection.createIndex({ field1: 1, field2: -1 })
Text index Creates an index on text content within a document. Enables full-text search and querying based on text content. db.collection.createIndex({ fieldName: "text" })
Geospatial index Creates an index for geographical data (points, lines, polygons). Optimizes queries based on location. db.collection.createIndex({ location: "2dsphere" })
Hashed index Creates a hashed index on a single field. Useful for evenly distributing data across buckets. db.collection.createIndex({ fieldName: "hashed" })
TTL index Automatically expires documents after a specified time. Useful for managing time-sensitive data. db.collection.createIndex({ expirationDate: 1 }, { expireAfterSeconds: 3600 })
//create an index
db.collectionName.createIndex({ name: 1 });

Indexes are data structures that improve query performance by creating an ordered list of values for one or more fields. They help MongoDB find specific documents more efficiently.