1
What type of database is MongoDB classified as?
A. Relational Database Management System (RDBMS)
B. NoSQL Document Database
C. Key-Value Store
D. Graph Database
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. Document
B. Field
C. Collection
D. Cluster
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. JSON
B. XML
C. BSON
D. YAML
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. id
B. _id
C. key
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. switch database_name
B. use database_name
C. connect database_name
D. db 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. It asks for confirmation to create it.
C. MongoDB creates the database automatically.
D. The data is discarded.
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. show dbs
B. list databases
C. display dbs
D. show databases all
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. Collection
B. Table
C. Document
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.createCollection('name')
C. db.newCollection('name')
D. db.insertCollection('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.delete()
B. db.users.drop()
C. db.users.remove()
D. db.dropCollection('users')
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.deleteDatabase()
B. db.dropDatabase()
C. db.removeDatabase()
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.insertOne()
C. db.collection.put()
D. db.collection.push()
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.insertMany({...}, {...})
B. db.collection.insertMany([{...}, {...}])
C. db.collection.addMany([{...}, {...}])
D. db.collection.insertAll([{...}, {...}])
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.getAll()
B. db.collection.select(*)
C. db.collection.find()
D. db.collection.search()
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().format()
B. db.collection.find().pretty()
C. db.collection.find().beautify()
D. db.collection.find().clean()
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. SELECT field
C. SHOW field
D. WHERE 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. $modify
B. $set
C. $change
D. $update
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. They function identically.
D. updateMany is deprecated.
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.erase()
B. db.collection.deleteOne()
C. db.collection.clear()
D. db.collection.zap()
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. 3306
B. 8080
C. 27017
D. 5432
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 username
B. The collection name
C. The database name
D. The server 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 Data Model
B. Object Document Mapper
C. Online Database Manager
D. Object Database Method
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. npm install mongo
B. npm install mongoose
C. npm add mongoose-db
D. node 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 configuration object defining the structure and rules of data.
D. A query builder.
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.Table()
B. new mongoose.Schema()
C. mongoose.createSchema()
D. mongoose.define()
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 compiled version of the Schema used to interact with the database.
C. A type of validation rule.
D. A JSON file storing data.
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. mongoose.model('User', userSchema)
B. mongoose.createModel('User', userSchema)
C. new mongoose.Model('User', userSchema)
D. userSchema.model('User')
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. mandatory: true
B. required: true
C. allowNull: false
D. mustHave: 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.save()
C. document.create()
D. document.push()
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.findId()
B. Model.findById()
C. Model.findOneId()
D. Model.get()
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. Integer
B. Number
C. Float
D. Int
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.db.connect(connectionString)
C. mongoose.start(connectionString)
D. mongoose.open(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. $lt
C. $gte
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 collection only.
B. To define a schema.
C. To instantiate and save a document in one step.
D. To create a database index.
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. findByIdAndUpdate
B. updateById
C. findAndModify
D. saveById
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 database server
B. The current database context
C. The collection list
D. The admin user
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. Returns all documents where age is 25.
C. Deletes documents where age is 25.
D. Updates 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. Restricts the size of the document.
B. Specifies the maximum number of documents to return.
C. Limits the number of fields returned.
D. Stops the query after a specific time.
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. Synchronous
B. Asynchronous
C. Blocking
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. { time: true }
B. { stamps: true }
C. { timestamps: true }
D. { dates: 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 creates a unique index to prevent duplicate values for that field.
B. It ensures the field cannot be null.
C. It hides the field from queries.
D. It makes the field read-only.
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 dynamic schemas; NoSQL has rigid schemas.
B. SQL has rigid schemas; NoSQL has dynamic/flexible 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 empty array []
B. null
C. undefined
D. An error
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. index.html
B. style.css
C. db.js or app.js
D. package.json
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. .sort({ field: -1 })
B. .sort({ field: 1 })
C. .sort({ field: 'desc' })
D. .order('down')
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.