Top MongoDB (Non-SQL) Interview Questions: Part 3
Today we are going to discuss the top 20 MongoDB Interview Questions. This is Part 3.
Question 11: How do you skip documents in a query in MongoDB?
Answer: To skip documents in a query in MongoDB, you can use the skip()
method. Here's an example:
javascriptCopy code
db.myCollection.find().skip(5)
Question 12: How do you perform a text search in MongoDB?
Answer: To perform a text search in MongoDB, you can use the $text
operator and create a text index. Here's an example:
javascriptCopy code
db.myCollection.find({ $text: { $search: "keyword" } })
Question 13: How do you perform transactions in MongoDB?
Answer: To perform transactions in MongoDB, you can use the session
object and the withTransaction()
method. Here's an example:
javascriptCopy code
session.startTransaction();
try {
// Perform operations
session.commitTransaction();
} catch (error) {
session.abortTransaction();
}
Question 14: How do you aggregate data from multiple collections in MongoDB?
Answer: To aggregate data from multiple collections in MongoDB, you can use the $lookup
stage in the aggregate()
method. Here's an example:
javascriptCopy code
db.myCollection.aggregate([
{
$lookup: {
from: "otherCollection",
localField: "id",
foreignField: "id",
as: "result"
}
}
])
Question 15: How do you create a backup of a MongoDB database?
Answer: To create a backup of a MongoDB database, you can use the mongodump
command-line tool. Here's an example:
cssCopy code
mongodump --db mydatabase --out /path/to/backup
if you have any questions or suggestions just do let me know on my Instagram or at codeculturepro@gmail.com