MongoDB Quiz - MCQ - Multiple Choice Questions

MongoDB is a popular open-source, NoSQL database that stores data in a flexible, JSON-like format called BSON. Unlike relational databases, which organize data in tables, MongoDB uses collections to hold documents, making it highly adaptable to diverse data structures.

As you embark on your journey to master MongoDB, this quiz will help test your knowledge of its fundamental concepts.

Note that each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. Which of the following best describes MongoDB?

A. Relational database
B. Spreadsheet program
C. Document-based NoSQL database
D. Graph database

Answer:

C. Document-based NoSQL database

Explanation:

MongoDB is a document-based NoSQL database, which means it stores data in BSON format (binary representation of JSON-like documents).

2. In MongoDB, a record is equivalent to a:

A. Row
B. Table
C. Document
D. Database

Answer:

C. Document

Explanation:

In MongoDB, a single entry or record is termed as a document.

3. Which of the following is the default port for MongoDB?

A. 27017
B. 8080
C. 3306
D. 5432

Answer:

A. 27017

Explanation:

MongoDB, by default, runs on port 27017.

4. Which MongoDB command is used to display the database you are currently using?

A. show currentDatabase
B. show db
C. use db
D. db

Answer:

D. db

Explanation:

Typing db in the MongoDB shell displays the name of the current database.

5. To create or switch to a database in MongoDB, which command would you use?

A. createDatabase(name)
B. switchDatabase(name)
C. use <database_name>
D. db.<database_name>

Answer:

C. use <database_name>

Explanation:

The use command is used to create or switch to a specified database.

6. Which of the following commands will show you all the collections in your current database?

A. show collections
B. list collections
C. display collections
D. db.collections()

Answer:

A. show collections

Explanation:

The show collections command lists all the collections in the current database.

7. How do you insert a new document into a collection in MongoDB?

A. db.<collection_name>.insert()
B. db.<collection_name>.newDocument()
C. db.<collection_name>.add()
D. db.<collection_name>.append()

Answer:

A. db.<collection_name>.insert()

Explanation:

The insert() method is used to add a new document into a specified collection.

8. Which of the following commands deletes a MongoDB database?

A. db.dropDatabase()
B. removeDatabase()
C. deleteDatabase()
D. destroyDatabase()

Answer:

A. db.dropDatabase()

Explanation:

The db.dropDatabase() command deletes the current database.

9. Which MongoDB method can be used to remove one or more documents from a collection?

A. db.<collection_name>.delete()
B. db.<collection_name>.remove()
C. db.<collection_name>.discard()
D. db.<collection_name>.drop()

Answer:

B. db.<collection_name>.remove()

Explanation:

The remove() method is used to delete documents from a collection based on specified criteria.

10. What is the BSON in MongoDB?

A. A database engine
B. A query language
C. A backup tool
D. Binary representation of JSON

Answer:

D. Binary representation of JSON

Explanation:

BSON stands for "Binary JSON". It's a binary representation of JSON-like documents, which MongoDB uses for storage and data transmission.

11. Which MongoDB command returns statistics about the database?

A. db.stats()
B. db.info()
C. db.data()
D. db.details()

Answer:

A. db.stats()

Explanation:

The db.stats() command returns a variety of statistics about the current database.

12. What format does MongoDB use for its queries?

A. SQL
B. XML
C. BSON
D. XQuery

Answer:

C. BSON

Explanation:

MongoDB uses BSON (Binary JSON) format for its queries and documents.

13. To update a document in a collection, which method is appropriate?

A. db.<collection_name>.modify()
B. db.<collection_name>.edit()
C. db.<collection_name>.revise()
D. db.<collection_name>.update()

Answer:

D. db.<collection_name>.update()

Explanation:

The update() method is used to modify existing documents within a collection based on specified criteria.

14. Which of the following operations provides a sorted list of the documents in a collection?

A. db.<collection_name>.sort()
B. db.<collection_name>.arrange()
C. db.<collection_name>.listSorted()
D. db.<collection_name>.orderBy()

Answer:

A. db.<collection_name>.sort()

Explanation:

The sort() method is used to return a sorted list of documents.

15. How can you backup your MongoDB database?

A. mongodump
B. mongobackup
C. mongosave
D. mongoarchive

Answer:

A. mongodump

Explanation:

The mongodump tool is used to create a binary export of the contents of a MongoDB database.

16. Which tool can be used to import content from a BSON file into a MongoDB database?

A. mongoimport
B. mongorestore
C. mongoload
D. mongofetch

Answer:

B. mongorestore

Explanation:

mongorestore is used to restore data from a binary database dump, i.e., from a BSON file.

17. Which of the following commands lists all available MongoDB databases?

A. show dbs
B. list dbs
C. show databases
D. db.list()

Answer:

A. show dbs

Explanation:

The show dbs command lists all available databases in MongoDB.

18. Which MongoDB function is used to limit the number of results returned?

A. db.<collection_name>.count()
B. db.<collection_name>.skip()
C. db.<collection_name>.limit()
D. db.<collection_name>.restrict()

Answer:

C. db.<collection_name>.limit()

Explanation:

The limit() function limits the number of results returned from a query.

19. In which language is MongoDB written?

A. Python
B. Java
C. C++
D. Go

Answer:

C. C++

Explanation:

MongoDB is written in C++.

20. If you wish to retrieve only the specified fields of a document, which method would you use?

A. project()
B. show()
C. select()
D. find()

Answer:

D. find()

Explanation:

You would use the find() method with a projection argument to retrieve only specified fields of a document.

21. Which of the following ensures atomic transactions in MongoDB?

A. WriteConcern
B. Sharding
C. Indexing
D. Replication

Answer:

A. WriteConcern

Explanation:

WriteConcern specifies the level of acknowledgment requested from MongoDB for write operations to ensure atomic transactions.

22. Which of the following is NOT a valid logical operator in MongoDB?

A. $and
B. $or
C. $nor
D. $between

Answer:

D. $between

Explanation:

MongoDB does not have a $between logical operator. For range queries, $gte and $lte are often used together.

23. If you want to join collections in MongoDB, which operator would you use?

A. $join
B. $link
C. $lookup
D. $merge

Answer:

C. $lookup

Explanation:

$lookup is used to perform a left outer join to another collection in the same database.

24. What type of index in MongoDB allows you to search text fields?

A. Text index
B. Compound index
C. Unique index
D. Sparse index

Answer:

A. Text index

Explanation:

A text index allows you to search for words and phrases in string content.

25. Which MongoDB command provides execution statistics about query performance?

A. db.<collection_name>.stats()
B. db.<collection_name>.explain()
C. db.<collection_name>.details()
D. db.<collection_name>.analyze()

Answer:

B. db.<collection_name>.explain()

Explanation:

The explain() method provides details about how MongoDB executed a query, which can be useful for optimizing performance.

26. How do you create a unique index on a field in MongoDB?

A. { uniqueKey: 1 }
B. { index: "unique" }
C. { type: "unique" }
D. { unique: true }

Answer:

D. { unique: true }

Explanation:

When creating an index, setting the unique option to true ensures that the indexed field contains unique values only.

27. Which MongoDB shell method is used to rename a collection?

A. db.<collection_name>.rename()
B. db.<collection_name>.renameCollection()
C. db.<collection_name>.changeName()
D. db.<collection_name>.alter()

Answer:

B. db.<collection_name>.renameCollection()

Explanation:

The renameCollection() method is used to rename a collection.

28. What does the mongos command do?

A. Starts the MongoDB server.
B. Starts a shard server.
C. Starts the MongoDB routing service.
D. Dumps the MongoDB database.

Answer:

C. Starts the MongoDB routing service.

Explanation:

mongos is the sharded cluster query router and is used to direct client requests to the appropriate shard(s).

29. In which format does MongoDB store its data?

A. XML
B. JSON
C. CSV
D. BSON

Answer:

D. BSON

Explanation:

MongoDB stores its data in BSON format, which is a binary representation of JSON-like documents.

30. Which command is used to terminate a long-running operation in MongoDB?

A. db.killOp()
B. db.stopOp()
C. db.terminate()
D. db.endOp()

Answer:

A. db.killOp()

Explanation:

The db.killOp() command can be used to terminate a long-running operation.


Comments