MongoDB Test 1 — Questions and Answers
Question 1: What type of database is MongoDB?
- Column Based
- Key Value Pair
- Graph Oriented
- Document Oriented (Correct answer)
Correct answer: Document Oriented
Explanation: <br> MongoDB, for example, is a document-oriented database that may be used on a variety of systems. According to its classification, MongoDB is a NoSQL database application that employs JSON-like documents with optional schemas.
Question 2: _____ is the default query document for find().
- {null}
- [] (Correct answer)
- null
- all of the above
Correct answer: []
In MongoDB, the `find()` method without arguments is used to retrieve all documents from a collection, which is internally equivalent to passing an empty query document `{}`. While `{}` is the standard BSON object for an empty query, the option `[]` might conceptually be chosen in some contexts to represent an absence of specific query criteria. However, it's crucial to note that `db.collection.find([])` is not valid syntax for retrieving all documents; the correct form is `db.collection.find({})`.
Question 3: The command to get a backup of a point-in-time data view is ____.
- fsync (Correct answer)
- mangodump
- sync
- dump
Correct answer: fsync
The `fsync` command in MongoDB, specifically `db.fsyncLock()`, is used to lock the database and flush all pending writes to disk. This action creates a consistent point-in-time snapshot of the data files, which can then be safely copied using file system tools for backup purposes. After copying, `db.fsyncUnlock()` releases the lock, allowing normal operations to resume.
Question 4: MongoDB adds a specific key to each document in a collection called _____ .
- _id (Correct answer)
- _sno
- _key
- _no
Correct answer: _id
MongoDB automatically adds an `_id` field to every document if one is not explicitly provided during insertion. This `_id` field serves as the primary key for the document within its collection, ensuring uniqueness and providing an efficient way to identify and retrieve individual documents. It is typically an ObjectId, a 12-byte BSON type.
Question 5: In a wrapped query, ______ is a useful option that sets the maximum number of documents to verify for a given query.
- scanmax
- scan
- maxscan (Correct answer)
- max
Correct answer: maxscan
The `maxscan` option in MongoDB queries limits the maximum number of documents that the database will scan on disk to fulfill a query. This is particularly useful for preventing long-running or inefficient queries that might scan an excessive number of documents, especially when an optimal index is not available. It helps control resource usage and improve query performance by setting an upper bound on the scan effort.
Question 6: ______ is the mongo shell command that lists database names.
- show database
- show dbs (Correct answer)
- show db
- none of the above
Correct answer: show dbs
The `show dbs` command is a standard utility in the MongoDB shell used to display a list of all available databases on the current MongoDB instance. It provides a quick overview of the databases present, along with their respective sizes. This command is essential for navigating and managing different databases within the shell environment.
Question 7: MongoDB's default listening port is _____ .
- 27017 (Correct answer)
- 85
- 8080
- 443
Correct answer: 27017
By default, the MongoDB daemon (`mongod`) listens for incoming connections on port 27017. This is the standard port that MongoDB clients, such as the `mongo` shell or application drivers, use to connect to a MongoDB instance. While configurable, 27017 is the widely recognized default for MongoDB services.
Question 8: In MongoDB, a collection and a document are identical to which of the SQL concepts?
- Database and Table
- Column and Row
- Table and Column
- Table and Row (Correct answer)
Correct answer: Table and Row
In the context of relational databases (SQL), a table is a structured set of data, similar to how a collection in MongoDB holds a group of related documents. A row in a SQL table represents a single record, which directly corresponds to a document in MongoDB, as each document is a self-contained unit of data. This analogy helps bridge the understanding between SQL and NoSQL concepts.
Question 9: In JavaScript, the data structure representation of a document is ____ .
- Object (Correct answer)
- Dictionary
- Hash
- Map
Correct answer: Object
MongoDB documents are stored in BSON (Binary JSON) format, which is a binary-encoded serialization of JSON-like documents. When interacting with MongoDB using the JavaScript-based mongo shell or JavaScript drivers, these BSON documents are naturally represented as JavaScript objects. JavaScript objects provide the key-value pair structure that perfectly maps to MongoDB's document model.
What type of database is MongoDB?