Tuesday, 18 July 2023

MongoDB Interview Questions - Sandeep Kanao

 

MongoDB Interview Questions - Sandeep Kanao

Question 1: What is MongoDB?

MongoDB is a NoSQL document-oriented database program. It uses JSON-like documents with optional schemas. It is an open-source, cross-platform, document-oriented database.

#Example
import pymongo

#establishing connection
client = pymongo.MongoClient("mongodb://localhost:27017/")

#creating database
db = client["mydatabase"]

#creating collection
col = db["customers"]

Question 2: What is the difference between SQL and NoSQL?

SQL databases are relational databases, while NoSQL databases are non-relational databases. SQL databases use tables to store data, while NoSQL databases use documents, key-value pairs, or graphs to store data.

#Example
#SQL
SELECT * FROM customers WHERE age > 18;

#NoSQL
db.customers.find({ "age": { "$gt": 18 } })

Question 3: What is a document in MongoDB?

A document in MongoDB is a record in a collection, which is similar to a row in a table in a relational database. It is a JSON-like data structure that can have fields, sub-documents, and arrays.

#Example
{
   "_id" : ObjectId("5f0c3d8f8f6d7a2d8c7f3a5e"),
   "name" : "John Doe",
   "age" : 25,
   "address" : {
      "street" : "123 Main St",
      "city" : "Anytown",
      "state" : "CA",
      "zip" : "12345"
   },
   "phone" : [
      {
         "type" : "home",
         "number" : "555-555-1234"
      },
      {
         "type" : "work",
         "number" : "555-555-5678"
      }
   ]
}

Question 4: What is a collection in MongoDB? - Sandeep Kanao

A collection in MongoDB is a group of documents that are stored together in a database. It is similar to a table in a relational database.

#Example
#creating collection
col = db["customers"]

Question 5: What is an index in MongoDB?

An index in MongoDB is a data structure that improves the speed of data retrieval operations on a collection. It is similar to an index in a book, which allows you to quickly find information.

#Example
#creating index
col.create_index("name")

Question 6: What is sharding in MongoDB?

Sharding in MongoDB is a method of distributing data across multiple servers. It allows you to scale horizontally by adding more servers to handle the load.

#Example
#enabling sharding
sh.enableSharding("mydatabase")

#sharding a collection
sh.shardCollection("mydatabase.customers", { "name": 1 })

Question 7: What is replication in MongoDB?

Replication in MongoDB is the process of synchronizing data across multiple servers. It allows you to create redundant copies of your data for increased availability and fault tolerance.

#Example
#creating replica set
rs.initiate()

#adding replica set members
rs.add("mongodb1.example.net")
rs.add("mongodb2.example.net")
rs.add("mongodb3.example.net")

Question 8: What is a cursor in MongoDB?

A cursor in MongoDB is a pointer to the result set of a query. It allows you to iterate over the results and retrieve them one at a time.

#Example
#finding documents
cursor = col.find()

#iterating over documents
for document in cursor:
    print(document)

Question 9: What is the $in operator in MongoDB?

The $in operator in MongoDB is used to match any of the values specified in an array. It is similar to the SQL IN operator.

#Example
#finding documents where age is 25 or 30
query = { "age": { "$in": [25, 30] } }
cursor = col.find(query)

Question 10: What is the $regex operator in MongoDB?

The $regex operator in MongoDB is used to perform regular expression matching on a field. It allows you to search for patterns in your data.

#Example
#finding documents where name starts with "J"
query = { "name": { "$regex": "^J" } }
cursor = col.find(query)

Question 11: What is the $group operator in MongoDB?

The $group operator in MongoDB is used to group documents by a specified field and perform aggregate functions on the grouped data. It is similar to the SQL GROUP BY clause.

#Example
#grouping documents by age and counting the number of documents in each group
query = [
    { "$group": { "_id": "$age", "count": { "$sum": 1 } } }
]
cursor = col.aggregate(query)

Question 12: What is the $lookup operator in MongoDB?

The $lookup operator in MongoDB is used to perform a left outer join between two collections. It allows you to combine data from multiple collections into a single result set.

#Example
#performing a left outer join between customers and orders collections
query = [
    {
        "$lookup": {
            "from": "orders",
            "localField": "_id",
            "foreignField": "customer_id",
            "as": "orders"
        }
    }
]
cursor = col.aggregate(query)

Question 13: What is the $unwind operator in MongoDB?

The $unwind operator in MongoDB is used to deconstruct an array field and output one document for each element in the array. It allows you to perform operations on the individual elements of an array.

#Example
#deconstructing the phone array field and outputting one document for each element
query = [
    { "$unwind": "$phone" }
]
cursor = col.aggregate(query)

Question 14: What is the $push operator in MongoDB?

The $push operator in MongoDB is used to add an element to an array field. It allows you to append data to an existing array.

#Example
#adding a new phone number to the phone array field
query = { "_id": ObjectId("5f0c3d8f8f6d7a2d8c7f3a5e") }
update = { "$push": { "phone": { "type": "mobile", "number": "555-555-7890" } } }
col.update_one(query, update)

Question 15: What is the $pull operator in MongoDB? - Sandeep Kanao

The $pull operator in MongoDB is used to remove an element from an array field. It allows you to delete data from an existing array.

#Example
#removing a phone number from the phone array field
query = { "_id": ObjectId("5f0c3d8f8f6d7a2d8c7f3a5e") }
update = { "$pull": { "phone": { "type": "work" } } }
col.update_one(query, update)

Question 16: What is the $set operator in MongoDB?

The $set operator in MongoDB is used to update the value of a field in a document. It allows you to modify existing data.

#Example
#updating the age field of a document
query = { "_id": ObjectId("5f0c3d8f8f6d7a2d8c7f3a5e") }
update = { "$set": { "age": 30 } }
col.update_one(query, update)

Question 17: What is the $unset operator in MongoDB?

The $unset operator in MongoDB is used to remove a field from a document. It allows you to delete data from a document.

#Example
#removing the address field from a document
query = { "_id": ObjectId("5f0c3d8f8f6d7a2d8c7f3a5e") }
update = { "$unset": { "address": "" } }
col.update_one(query, update)

Question 18: What is the $rename operator in MongoDB?

The $rename operator in MongoDB is used to rename a field in a document. It allows you to change the name of a field.

#Example
#renaming the age field to years_old
query = { "_id": ObjectId("5f0c3d8f8f6d7a2d8c7f3a5e") }
update = { "$rename": { "age": "years_old" } }
col.update_one(query, update)

Question 19: What is the $inc operator in MongoDB? - Sandeep Kanao

The $inc operator in MongoDB is used to increment the value of a field in a document. It allows you to perform arithmetic operations on a field.

#Example
#incrementing the age field of a document by 1
query = { "_id": ObjectId("5f0c3d8f8f6d7a2d8c7f3a5e") }
update = { "$inc": { "age": 1 } }
col.update_one(query, update)

Question 20: What is the $currentDate operator in MongoDB? - Sandeep Kanao

The $currentDate operator in MongoDB is used to set the value of a field to the current date and time. It allows you to track when a document was last modified.

#Example
#setting the last_modified field of a document to the current date and time
query = { "_id": ObjectId("5f0c3d8f8f6d7a2d8c7f3a5e") }
update = { "$currentDate": { "last_modified": True } }
col.update_one(query, update)

No comments:

Post a Comment