Create New Post

MongoDB - MCQ with answer

1. What is MongoDB?

a. A relational database
b. A NoSQL database
c. A graph database
d. A file system

Answer: b. A NoSQL database

2. In MongoDB, what is a document?

a. A record in a table
b. A field in a collection
c. A JSON-like data structure
d. A row in a database

Answer: c. A JSON-like data structure

3. What is the default port for MongoDB?

a. 3306
b. 27017
c. 5432
d. 8080

Answer: b. 27017

4. Which data format is used in MongoDB for storage and data exchange?

a. XML
b. JSON
c. BSON
d. YAML

Answer: c. BSON

5. What is a Collection in MongoDB?

a. A group of related documents
b. A set of fields
c. A single document
d. A table in a relational database

Answer: a. A group of related documents

6. What is the purpose of the _id field in MongoDB?

a. It is a reserved keyword
b. It must be manually added to each document
c. It is automatically added to each document as a unique identifier
d. It is used only for indexing

Answer: c. It is automatically added to each document as a unique identifier

7. How can you update a document in MongoDB?

a. db.collection.update()
b. db.collection.modify()
c. db.collection.save()
d. db.collection.edit()

Answer: a. db.collection.update()

8. What does the $set operator do in MongoDB?

a. Adds a new field to a document
b. Removes a field from a document
c. Updates a field with a new value
d. Replaces the entire document

Answer: c. Updates a field with a new value

9. What does the $elemMatch operator do in MongoDB?

a. Matches documents based on an element in an array
b. Adds a new element to an array
c. Removes an element from an array
d. Sorts elements in an array

Answer: a. Matches documents based on an element in an array

10. What is the purpose of the mongod process in MongoDB?

a. It is the MongoDB shell
b. It is the MongoDB server
c. It is the MongoDB router
d. It is the MongoDB driver

Answer: b. It is the MongoDB server

11. How can you create an index in MongoDB?

a. db.collection.createIndex()
b. db.createIndex()
c. db.collection.addIndex()
d. db.addIndex()

Answer: a. db.collection.createIndex()

12. What is Sharding in MongoDB?

a. Document validation
b. Data replication
c. Data partitioning
d. Database encryption

Answer: c. Data partitioning

13. What does the $push operator do in MongoDB?

a. Updates documents
b. Adds elements to an array
c. Removes elements from an array
d. Creates a new collection

Answer: b. Adds elements to an array

14. In MongoDB, what is the purpose of the mongos process?

a. It is the MongoDB shell
b. It is the MongoDB server
c. It is the MongoDB router
d. It is the MongoDB driver

Answer: c. It is the MongoDB router

15. How can you enable authentication in MongoDB?

a. Start mongod with the --auth option
b. Use the enableAuth() method
c. Set the authentication flag in the configuration file
d. Authentication is enabled by default

Answer: a. Start mongod with the --auth option

16. What does the MongoDB profiler do?

a. Manages connections to the database
b. Analyzes and optimizes query performance
c. Enforces data validation rules
d. Provides real-time data backups

Answer: b. Analyzes and optimizes query performance

17. What is a covered query in MongoDB?

a. A query that includes all fields of a document
b. A query that can be fulfilled using an index only
c. A query for encryption purposes
d. A query that includes only specific fields

Answer: b. A query that can be fulfilled using an index only

18. What is MongoDB Atlas?

a. A graphical user interface for MongoDB
b. An in-memory storage engine for MongoDB
c. A fully managed cloud database service
d. A standalone MongoDB shell

Answer: c. A fully managed cloud database service

19. What is GridFS used for in MongoDB?

a. Document validation
b. Storing large files by dividing them into smaller chunks
c. Data replication
d. Sharding

Answer: b. Storing large files by dividing them into smaller chunks

20. How do you perform aggregation in MongoDB?

a. Using the $aggregate operator
b. Using the aggregate() method
c. Using the $group operator
d. Aggregation is not supported in MongoDB

Answer: b. Using the aggregate() method

21. What is the purpose of the $pull operator in MongoDB?

a. Adds elements to an array
b. Removes elements from an array
c. Updates documents
d. Replaces the entire document

Answer: b. Removes elements from an array

22. Which operator is used to perform equality matching on arrays in MongoDB?

a. $in
b. $eq
c. $ne
d. $all

Answer: b. $eq

23. What does the db.getCollectionNames() method do in MongoDB?

a. Retrieves the list of databases
b. Retrieves the list of collections in the current database
c. Retrieves the list of documents in a collection
d. Retrieves the list of fields in a document

Answer: b. Retrieves the list of collections in the current database

24. What is the purpose of the $addToSet operator in MongoDB?

a. Adds elements to an array
b. Removes elements from an array
c. Adds elements to an array only if they do not already exist
d. Updates documents

Answer: c. Adds elements to an array only if they do not already exist

25. How can you find distinct values in a field using MongoDB?

a. db.collection.distinct()
b. db.collection.findDistinct()
c. db.distinct()
d. db.findDistinct()

Answer: a. db.collection.distinct()

26. What does the explain() method do in MongoDB?

a. Displays information about the database schema
b. Displays detailed information about the execution of a query
c. Executes a query and displays the result
d. Exports the data in the collection to a file

Answer: b. Displays detailed information about the execution of a query

27. What is the purpose of the $exists operator in MongoDB?

a. Matches documents where a field does not exist
b. Matches documents where a field exists
c. Matches documents based on the existence of an array
d. Matches documents based on the absence of a specific field

Answer: b. Matches documents where a field exists

28. How can you sort the results of a query in descending order in MongoDB?

a. db.collection.sort({field: -1})
b. db.collection.orderBy({field: -1})
c. db.collection.find().sort({field: -1})
d. db.collection.find().orderBy({field: -1})

Answer: c. db.collection.find().sort({field: -1})

29. Which of the following is true about the $regex operator in MongoDB?

a. It is used for adding regular expressions to documents
b. It is used for searching text within documents using regular expressions
c. It is used for updating documents with regular expressions
d. It is used for removing regular expressions from documents

Answer: b. It is used for searching text within documents using regular expressions

30. What is the purpose of the mongorestore tool in MongoDB?

a. It is used to export data from MongoDB
b. It is used to query the MongoDB database
c. It is used to restore a previously created MongoDB database backup
d. It is used to create a new MongoDB collection

Answer: c. It is used to restore a previously created MongoDB database backup

31. What does the $and operator do in MongoDB?

a. Performs a logical AND operation on an array of expressions
b. Adds elements to an array
c. Removes elements from an array
d. Performs a logical OR operation on an array of expressions

Answer: a. Performs a logical AND operation on an array of expressions

32. How do you remove documents from a collection in MongoDB?

a. db.collection.delete()
b. db.collection.remove()
c. db.collection.drop()
d. db.collection.truncate()

Answer: b. db.collection.remove()

33. What is the purpose of the $slice operator in MongoDB?

a. To slice arrays within documents
b. To slice documents within a collection
c. To slice fields within documents
d. To slice collections within a database

Answer: a. To slice arrays within documents

34. Which of the following is a valid projection in MongoDB?

a. db.collection.find({}, {field1: 1, field2: 0})
b. db.collection.find({}, {field1: true, field2: false})
c. db.collection.find({}, {field1: include, field2: exclude})
d. db.collection.find({}, {field1: "include", field2: "exclude"})

Answer: b. db.collection.find({}, {field1: true, field2: false})

35. What is the purpose of the mongotop tool in MongoDB?

a. It is used to perform aggregation on MongoDB collections
b. It is used to monitor disk I/O
c. It is used to monitor MongoDB server operations
d. It is used to export data from MongoDB to a file

Answer: c. It is used to monitor MongoDB server operations

36. How can you limit the number of documents returned in a MongoDB query?

a. db.collection.limit(5)
b. db.collection.find().limit(5)
c. db.collection.setLimit(5)
d. db.collection.find().setLimit(5)

Answer: b. db.collection.find().limit(5)

37. What is the purpose of the mongodump tool in MongoDB?

a. It is used to import data into MongoDB
b. It is used to query the MongoDB database
c. It is used to create a backup of a MongoDB database
d. It is used to delete data from MongoDB

Answer: c. It is used to create a backup of a MongoDB database

38. What is the purpose of the $sum operator in MongoDB Aggregation?

a. To calculate the average value of a field
b. To concatenate strings in a field
c. To count the number of documents
d. To calculate the sum of numeric values in a field

Answer: d. To calculate the sum of numeric values in a field

39. Which method is used to create an index on multiple fields in MongoDB?

a. db.collection.createIndex({field1: 1, field2: 1})
b. db.collection.createIndex({field1: "asc", field2: "asc"})
c. db.collection.createIndex({field1: "asc", field2: "desc"})
d. db.collection.createIndex({field1: 1}, {field2: 1})

Answer: a. db.collection.createIndex({field1: 1, field2: 1})

40. What is the purpose of the $out stage in MongoDB Aggregation?

a. To append documents to an existing collection
b. To create a new collection with the aggregated results
c. To update existing documents in a collection
d. To remove documents from a collection

Answer: b. To create a new collection with the aggregated results

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

84705