1
What type of database is MongoDB classified as?
A. Graph Database
B. Key-Value Store
C. NoSQL Document Database
D. Relational Database Management System (RDBMS)
Reveal Answer
Hide Answer
Correct Answer: NoSQL Document Database
Explanation:
MongoDB is a NoSQL database that stores data in flexible, JSON-like documents.
2
In MongoDB, what is the equivalent of a 'Table' in an RDBMS?
A. Cluster
B. Document
C. Field
D. Collection
Reveal Answer
Hide Answer
Correct Answer: Collection
Explanation:
A collection in MongoDB is a group of documents, functioning similarly to a table in relational databases.
3
Which format does MongoDB use to store data internally?
A. XML
B. YAML
C. BSON
D. JSON
Reveal Answer
Hide Answer
Correct Answer: BSON
Explanation:
MongoDB uses BSON (Binary JSON), which allows for faster traversal and support for more data types than standard JSON.
4
What is the default field added to every MongoDB document to act as a primary key?
A. key
B. id
C. _id
D. primary_id
Reveal Answer
Hide Answer
Correct Answer: _id
Explanation:
MongoDB automatically assigns a unique _id field (ObjectId) to every document if one is not provided.
5
Which command is used in the MongoDB shell to switch to a specific database?
A. db database_name
B. use database_name
C. connect database_name
D. switch database_name
Reveal Answer
Hide Answer
Correct Answer: use database_name
Explanation:
The use command allows you to switch context to a specific database or create it if it doesn't exist.
6
What happens if you switch to a database that does not exist and insert data?
A. It throws an error.
B. The data is discarded.
C. It asks for confirmation to create it.
D. MongoDB creates the database automatically.
Reveal Answer
Hide Answer
Correct Answer: MongoDB creates the database automatically.
Explanation:
MongoDB creates databases lazily; the database is created only when the first collection or document is inserted.
7
Which command displays the list of existing databases in the shell?
A. list databases
B. show databases all
C. show dbs
D. display dbs
Reveal Answer
Hide Answer
Correct Answer: show dbs
Explanation:
show dbs (or show databases) lists all databases on the server that contain data.
8
In MongoDB, a 'Row' in an RDBMS is equivalent to a:
A. Document
B. Collection
C. Table
D. Field
Reveal Answer
Hide Answer
Correct Answer: Document
Explanation:
A document is a single record in a collection, analogous to a row in a relational table.
9
Which command is used to explicitly create a collection with specific options?
A. db.collection.create('name')
B. db.insertCollection('name')
C. db.newCollection('name')
D. db.createCollection('name')
Reveal Answer
Hide Answer
Correct Answer: db.createCollection('name')
Explanation:
db.createCollection() is used to explicitly create a collection, often to set validation rules or capped collection settings.
10
How do you delete a collection named 'users' in the MongoDB shell?
A. db.users.remove()
B. db.dropCollection('users')
C. db.users.delete()
D. db.users.drop()
Reveal Answer
Hide Answer
Correct Answer: db.users.drop()
Explanation:
The drop() method is called on a collection object to remove it entirely from the database.
11
Which command deletes the currently selected database?
A. db.removeDatabase()
B. db.dropDatabase()
C. db.deleteDatabase()
D. delete db
Reveal Answer
Hide Answer
Correct Answer: db.dropDatabase()
Explanation:
db.dropDatabase() removes the current database and its associated files.
12
Which method is used to insert a single document into a collection?
A. db.collection.addOne()
B. db.collection.put()
C. db.collection.push()
D. db.collection.insertOne()
Reveal Answer
Hide Answer
Correct Answer: db.collection.insertOne()
Explanation:
insertOne() is the standard method for inserting a single document into a collection.
13
What is the correct syntax to insert multiple documents at once?
A. db.collection.insertAll([{...}, {...}])
B. db.collection.insertMany({...}, {...})
C. db.collection.addMany([{...}, {...}])
D. db.collection.insertMany([{...}, {...}])
Reveal Answer
Hide Answer
Correct Answer: db.collection.insertMany([{...}, {...}])
Explanation:
insertMany() accepts an array of documents to insert into the collection.
14
Which method retrieves all documents from a collection?
A. db.collection.find()
B. db.collection.search()
C. db.collection.select(*)
D. db.collection.getAll()
Reveal Answer
Hide Answer
Correct Answer: db.collection.find()
Explanation:
find() without arguments returns a cursor to all documents in the collection.
15
How do you format the output of a find() query to be more readable in the shell?
A. db.collection.find().clean()
B. db.collection.find().pretty()
C. db.collection.find().beautify()
D. db.collection.find().format()
Reveal Answer
Hide Answer
Correct Answer: db.collection.find().pretty()
Explanation:
The .pretty() method formats the resulting JSON output with indentation and line breaks.
16
Which operator is used to specify fields to include or exclude in a query result (Projection)?
A. { field: 1 }
B. WHERE field
C. SELECT field
D. SHOW field
Reveal Answer
Hide Answer
Correct Answer: { field: 1 }
Explanation:
In the second argument of find(), setting a field to 1 includes it, while 0 excludes it.
17
Which update operator is used to modify the value of a specific field?
A. $change
B. $modify
C. $update
D. $set
Reveal Answer
Hide Answer
Correct Answer: $set
Explanation:
The $set operator replaces the value of a field with the specified value.
18
What is the difference between updateOne() and updateMany()?
A. updateOne updates the first match; updateMany updates all matches.
B. updateOne updates the last match; updateMany updates all matches.
C. updateMany is deprecated.
D. They function identically.
Reveal Answer
Hide Answer
Correct Answer: updateOne updates the first match; updateMany updates all matches.
Explanation:
updateOne affects only the first document matching the filter, while updateMany affects all matching documents.
19
Which method is used to remove a document from a collection?
A. db.collection.deleteOne()
B. db.collection.zap()
C. db.collection.clear()
D. db.collection.erase()
Reveal Answer
Hide Answer
Correct Answer: db.collection.deleteOne()
Explanation:
deleteOne() removes the first document that matches the filter criteria.
20
What is the default TCP port for MongoDB?
A. 27017
B. 8080
C. 5432
D. 3306
Reveal Answer
Hide Answer
Correct Answer: 27017
Explanation:
27017 is the standard default port for a MongoDB instance.
21
In a MongoDB connection string mongodb://localhost:27017/mydb, what does mydb represent?
A. The collection name
B. The server name
C. The username
D. The database name
Reveal Answer
Hide Answer
Correct Answer: The database name
Explanation:
The path segment following the host and port in the connection string specifies the database to connect to.
22
What does ODM stand for in the context of Mongoose?
A. Object Database Method
B. Object Document Mapper
C. Online Database Manager
D. Object Data Model
Reveal Answer
Hide Answer
Correct Answer: Object Document Mapper
Explanation:
Mongoose is an ODM (Object Document Mapper) that maps JavaScript objects to MongoDB documents.
23
Which command installs Mongoose in a Node.js project?
A. node install mongoose
B. npm add mongoose-db
C. npm install mongo
D. npm install mongoose
Reveal Answer
Hide Answer
Correct Answer: npm install mongoose
Explanation:
The standard NPM command to install the Mongoose library is npm install mongoose.
24
In Mongoose, what is a 'Schema'?
A. A direct connection to the database.
B. The actual document stored in MongoDB.
C. A query builder.
D. A configuration object defining the structure and rules of data.
Reveal Answer
Hide Answer
Correct Answer: A configuration object defining the structure and rules of data.
Explanation:
A Schema defines the shape of the documents within a collection, including types, defaults, and validation.
25
Which function is used to create a Mongoose Schema?
A. new mongoose.Schema()
B. new mongoose.Table()
C. mongoose.define()
D. mongoose.createSchema()
Reveal Answer
Hide Answer
Correct Answer: new mongoose.Schema()
Explanation:
You define a schema by instantiating new mongoose.Schema({...}).
26
What is a Mongoose 'Model'?
A. A prototype of the database server.
B. A JSON file storing data.
C. A compiled version of the Schema used to interact with the database.
D. A type of validation rule.
Reveal Answer
Hide Answer
Correct Answer: A compiled version of the Schema used to interact with the database.
Explanation:
A Model is a class constructed from a Schema that provides an interface to the database (CRUD operations).
27
How do you define a model named 'User' based on a schema userSchema?
A. userSchema.model('User')
B. mongoose.createModel('User', userSchema)
C. new mongoose.Model('User', userSchema)
D. mongoose.model('User', userSchema)
Reveal Answer
Hide Answer
Correct Answer: mongoose.model('User', userSchema)
Explanation:
mongoose.model() is the method used to create a model from a name and a schema.
28
In a Mongoose Schema, how do you ensure a field is mandatory?
A. mustHave: true
B. allowNull: false
C. required: true
D. mandatory: true
Reveal Answer
Hide Answer
Correct Answer: required: true
Explanation:
The required: true property in a schema definition enforces that the field must be present.
29
Which method is used to save a new document instance created via Mongoose?
A. document.insert()
B. document.push()
C. document.save()
D. document.create()
Reveal Answer
Hide Answer
Correct Answer: document.save()
Explanation:
When you create a new instance of a Model, you call .save() on that instance to persist it to the database.
30
Which Mongoose method finds a document by its _id?
A. Model.findOneId()
B. Model.findId()
C. Model.get()
D. Model.findById()
Reveal Answer
Hide Answer
Correct Answer: Model.findById()
Explanation:
findById(id) is a convenience method provided by Mongoose to retrieve a document by its primary key.
31
What data type should be defined in a Mongoose schema for an age field?
A. Int
B. Number
C. Float
D. Integer
Reveal Answer
Hide Answer
Correct Answer: Number
Explanation:
Mongoose uses standard JavaScript types and specific Mongoose types. Number covers integers and floats.
32
How do you connect to MongoDB using Mongoose?
A. mongoose.connect(connectionString)
B. mongoose.open(connectionString)
C. mongoose.start(connectionString)
D. mongoose.db.connect(connectionString)
Reveal Answer
Hide Answer
Correct Answer: mongoose.connect(connectionString)
Explanation:
mongoose.connect() is the method used to establish a connection to the MongoDB instance.
33
Which query operator is used in MongoDB to find values 'greater than' a specific value?
A. $gt
B. $gte
C. $lt
D. $high
Reveal Answer
Hide Answer
Correct Answer: $gt
Explanation:
$gt stands for 'greater than'.
34
What is the purpose of Model.create() in Mongoose?
A. To create a database index.
B. To define a schema.
C. To create a collection only.
D. To instantiate and save a document in one step.
Reveal Answer
Hide Answer
Correct Answer: To instantiate and save a document in one step.
Explanation:
Model.create(data) is a shortcut for creating a new instance and calling .save() immediately.
35
When defining a schema, what does default: Date.now do?
A. Sets the field to the current date if no value is provided.
B. Forces the user to enter the current date.
C. Updates the date every time the document is accessed.
D. Throws an error if the date is not now.
Reveal Answer
Hide Answer
Correct Answer: Sets the field to the current date if no value is provided.
Explanation:
The default property specifies a value (or function returning a value) to use if the field is missing during creation.
36
Which Mongoose method is used to find a document by ID and update it?
A. updateById
B. findAndModify
C. saveById
D. findByIdAndUpdate
Reveal Answer
Hide Answer
Correct Answer: findByIdAndUpdate
Explanation:
findByIdAndUpdate finds a matching document, updates it according to the update object, and optionally returns the new or old document.
37
In the MongoDB shell, what does db refer to?
A. The admin user
B. The collection list
C. The database server
D. The current database context
Reveal Answer
Hide Answer
Correct Answer: The current database context
Explanation:
The variable db in the shell refers to the database currently selected via the use command.
38
What is the result of db.collection.find({ age: 25 })?
A. Returns the first document where age is 25.
B. Updates documents where age is 25.
C. Returns all documents where age is 25.
D. Deletes documents where age is 25.
Reveal Answer
Hide Answer
Correct Answer: Returns all documents where age is 25.
Explanation:
find with a query object returns a cursor for all documents matching the criteria.
39
Which of the following is NOT a valid BSON data type?
A. String
B. Boolean
C. Table
D. Array
Reveal Answer
Hide Answer
Correct Answer: Table
Explanation:
Table is a relational database concept; it is not a BSON data type. BSON supports Arrays, Strings, Booleans, Objects, etc.
40
What does the limit() method do in a MongoDB query?
A. Stops the query after a specific time.
B. Limits the number of fields returned.
C. Specifies the maximum number of documents to return.
D. Restricts the size of the document.
Reveal Answer
Hide Answer
Correct Answer: Specifies the maximum number of documents to return.
Explanation:
Appended to a find() query, limit(n) ensures only n documents are returned.
41
In Node.js, most MongoDB operations are:
A. Asynchronous
B. Blocking
C. Synchronous
D. Linear
Reveal Answer
Hide Answer
Correct Answer: Asynchronous
Explanation:
Database operations in Node.js (via driver or Mongoose) are asynchronous and return Promises or use callbacks.
42
Which Mongoose schema option adds createdAt and updatedAt fields automatically?
A. { dates: true }
B. { time: true }
C. { stamps: true }
D. { timestamps: true }
Reveal Answer
Hide Answer
Correct Answer: { timestamps: true }
Explanation:
Passing { timestamps: true } as the second argument to the Schema constructor automatically manages creation and update timestamps.
43
What is the purpose of the unique: true option in a Mongoose schema field?
A. It ensures the field cannot be null.
B. It creates a unique index to prevent duplicate values for that field.
C. It makes the field read-only.
D. It hides the field from queries.
Reveal Answer
Hide Answer
Correct Answer: It creates a unique index to prevent duplicate values for that field.
Explanation:
unique: true tells MongoDB to create a unique index, ensuring no two documents have the same value for that field.
44
Which method effectively removes a document by ID in Mongoose?
A. findByIdAndRemove() (or findByIdAndDelete)
B. removeById()
C. deleteId()
D. dropId()
Reveal Answer
Hide Answer
Correct Answer: findByIdAndRemove() (or findByIdAndDelete)
Explanation:
findByIdAndDelete (and the older findByIdAndRemove) is used to find a document by its ID and remove it from the collection.
45
What is the structural difference between SQL and NoSQL regarding schemas?
A. SQL has rigid schemas; NoSQL has dynamic/flexible schemas.
B. SQL has dynamic schemas; NoSQL has rigid schemas.
C. Both use rigid schemas.
D. Both use dynamic schemas.
Reveal Answer
Hide Answer
Correct Answer: SQL has rigid schemas; NoSQL has dynamic/flexible schemas.
Explanation:
SQL requires a predefined table structure. MongoDB (NoSQL) allows documents in the same collection to have different fields (though Mongoose enforces a schema at the application level).
46
To perform an update that increments a number field by 1, which operator is used?
A. $add
B. $inc
C. $plus
D. $sum
Reveal Answer
Hide Answer
Correct Answer: $inc
Explanation:
The $inc operator increments a field by a specified value.
47
What does the findOne method return if no document matches?
A. An error
B. undefined
C. null
D. An empty array []
Reveal Answer
Hide Answer
Correct Answer: null
Explanation:
findOne returns a single document object if found, or null if no match is found.
48
Which file usually contains the database connection logic in a Node/Express app?
A. style.css
B. package.json
C. index.html
D. db.js or app.js
Reveal Answer
Hide Answer
Correct Answer: db.js or app.js
Explanation:
Backend logic, including database connections, is typically found in the main application file (app.js, server.js) or a dedicated config file (db.js).
49
In a Mongoose query, how do you specify that you want to sort the results in descending order?
A. .order('down')
B. .sort({ field: -1 })
C. .sort({ field: 'desc' })
D. .sort({ field: 1 })
Reveal Answer
Hide Answer
Correct Answer: .sort({ field: -1 })
Explanation:
In MongoDB/Mongoose sorting, 1 represents ascending order and -1 represents descending order.
50
If you want to define an array of strings in a Mongoose schema, what is the correct syntax?
A. tags: [String]
B. tags: { type: Array, contains: String }
C. tags: String[]
D. tags: Array<String>
Reveal Answer
Hide Answer
Correct Answer: tags: [String]
Explanation:
Mongoose uses the syntax [String] (or [Number], etc.) to define an array of a specific type.