mongodb banner

MongoDB Multiple Choice Questions (MCQs) and Answers

Master MongoDB with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of MongoDB. Begin your placement preparation journey now!

Q31

Q31 What is the primary benefit of embedding documents in MongoDB?

A

To speed up queries

B

To enforce data consistency

C

To reduce data duplication

D

To simplify query syntax

Q32

Q32 In MongoDB, when should you use references instead of embedding?

A

When data size is small

B

When data is frequently updated

C

When data is rarely accessed

D

When data duplication is a concern

Q33

Q33 How does MongoDB handle schema validation?

A

Automatically upon every insert and update

B

Only during collection creation

C

Through user-defined validation rules

D

It does not support schema validation

Q34

Q34 What is the advantage of using a normalized data model in MongoDB?

A

Improved write performance

B

Increased data redundancy

C

Reduced data redundancy

D

Simplified queries

Q35

Q35 In MongoDB, what is a potential drawback of deeply nested documents?

A

Increased query performance

B

Simplified data retrieval

C

Limitations in depth and size

D

Improved data consistency

Q36

Q36 Which command can be used to create a schema validation rule in MongoDB?

A

db.createCollection("users", {validator: {...}})

B

db.users.setSchema({...})

C

db.users.validateSchema({...})

D

db.schema.create({...})

Q37

Q37 How can you enforce that all documents in the 'books' collection have a 'title' field in MongoDB?

A

db.books.update({}, {$set: {title: {$exists: true}}})

B

db.books.createIndex({title: 1}, {unique: true})

C

db.books.setValidator({title: {$exists: true}})

D

db.books.modifySchema({title: {$exists: true}})

Q38

Q38 What does this MongoDB command do:
db.runCommand({collMod: "mycollection", validator: {...}})?

A

Modifies an existing collection's schema

B

Deletes and recreates the collection with a new schema

C

Creates a new collection

D

Runs a database maintenance operation

Q39

Q39 How would you add a field with a default value to an existing MongoDB collection?

A

db.collection.updateMany({}, {$set: {newField: "defaultValue"}})

B

db.collection.setDefault({newField: "defaultValue"})

C

db.collection.addField("newField", "defaultValue")

D

db.collection.modify({}, {$setDefault: {newField: "defaultValue"}})

Q40

Q40 Identify the issue:
db.createCollection("users", {validation: {age: {$type: "int"}}})

A

Incorrect syntax for schema validation

B

The 'age' field type should be 'number'

C

The 'users' collection already exists

D

No issue

Q41

Q41 What's wrong with this schema validation rule:
{validator: {email: {$regex: /@mongodb.com$/}}}?

A

The regex is incorrect

B

The validator should be set on the collection level

C

The field 'email' doesn't exist

D

No issue

Q42

Q42 Why might db.users.insert({name: "John", age: "30"}) fail in a collection with schema validation?

A

The 'age' field is not an integer

B

The 'name' field is duplicated

C

There's no 'age' field in the schema

D

There's no issue

Q43

Q43 Spot the error in nested schema validation:
{validator: {address: {city: {$type: "string"}}}}

A

'address' must be an embedded document

B

'city' is not a valid field type

C

Nested validation is not supported

D

No error

Q44

Q44 What is the primary purpose of indexing in MongoDB?

A

To increase data storage efficiency

B

To enhance security

C

To speed up query performance

D

To simplify data aggregation

Q45

Q45 What is a compound index in MongoDB?

A

An index on a single field

B

An index combining multiple fields

C

A unique index

D

A text index

Q46

Q46 What happens when you create an index on a field that has duplicate values in MongoDB?

A

The index creation fails

B

The duplicate values are removed

C

Only the first occurrence is indexed

D

The index is created with no issues

Q47

Q47 In MongoDB, what is a covered query?

A

A query that only needs indexes to return results

B

A query that retrieves all fields of a document

C

A query that updates data

D

A query that requires sorting

Q48

Q48 How does the $group stage in the aggregation pipeline operate?

A

It sorts the documents

B

It filters the documents

C

It groups documents by specified criteria

D

It merges documents into a single document

Q49

Q49 What is the impact of indexing on write performance in MongoDB?

A

It improves write performance

B

It has no impact

C

It can slow down write performance

D

It varies depending on the document size

Q50

Q50 Which command creates a simple index on the 'name' field in MongoDB?

A

db.collection.createIndex({name: 1})

B

db.collection.index({name: 1})

C

db.collection.addIndex({name: 1})

D

db.collection.setIndex({name: 1})

Q51

Q51 How do you create a descending index on the 'age' field in MongoDB?

A

db.collection.createIndex({age: -1})

B

db.collection.createIndex({age: "desc"})

C

db.collection.createIndex({age: 0})

D

db.collection.createIndex({age: "descending"})

Q52

Q52 What is the purpose of the $match stage in MongoDB's aggregation pipeline?

A

To sort documents

B

To group documents

C

To filter documents based on criteria

D

To limit the number of documents

Q53

Q53 How can you create a text index on two fields, 'title' and 'description', in MongoDB?

A

db.collection.createIndex({title: "text", description: "text"})

B

db.collection.createTextIndex({title, description})

C

db.collection.addTextIndex({title, description})

D

db.collection.createIndex({title: 1, description: 1}, {type: "text"})

Q54

Q54 Identify the error in this command:
db.collection.createIndex({name: "1"})

A

Incorrect index type

B

Incorrect field name

C

Syntax error in the index specification

D

No error

Q55

Q55 Why might an aggregation pipeline return incorrect results after adding a new index?

A

The index is not being used

B

The index is corrupt

C

The index has caused a change in the data order

D

The pipeline does not support indexing

Q56

Q56 What could be wrong if db.collection.createIndex({email: 1}, {unique: true}) fails to enforce uniqueness?

A

The collection already has duplicate emails

B

The index is not properly configured

C

The 'email' field is not indexed

D

MongoDB does not support unique indexes

Q57

Q57 Spot the error:
An aggregation pipeline with $sort, $match, and $group stages returns no results.

A

The $sort stage is misplaced

B

The $match stage is using incorrect criteria

C

The $group stage has an error in the grouping key

D

No error

Q58

Q58 Which operator is used in MongoDB to select documents where a field equals a specified value?

A

$eq

B

$set

C

$match

D

$get

Q59

Q59 What does the $in operator do in a MongoDB query?

A

Selects documents where a field's value is any of the specified array values

B

Changes the value of a field to one within a specified array

C

Counts how many times a field's value appears in a specified array

D

None of the above

Q60

Q60 What is the purpose of the $and operator in MongoDB queries?

A

To perform a logical AND operation on an array of two or more expressions

B

To compare two fields in the same document

C

To add new fields to documents

D

To update data in a document conditionally

ad verticalad vertical
ad