MongoDB MCQs – MongoDB Basics

11.) Which of the following commands deletes a collection?

A) db.collection.delete()
B) db.collection.remove()
C) db.collection.drop()
D) db.removeCollection()

Answer: Option C

Explanation: The drop() method removes a collection entirely.

12.) Which of the following is used to update a document in a collection?

A) db.collection.update()
B) db.collection.modify()
C) db.collection.findAndUpdate()
D) db.collection.save()

Answer: Option A

Explanation: The update() method modifies existing documents in a collection.

13.) Which method retrieves only specific fields from documents?

A) db.collection.filter()
B) db.collection.projection()
C) db.collection.select()
D) db.collection.find({}, {field: 1})

Answer: Option D

Explanation: MongoDB’s projection specifies which fields to include or exclude in the result set.

14.) Which of the following commands drops a database?

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

Answer: Option A

Explanation: The dropDatabase() method deletes the current database.

15.) What is the default identifier for every MongoDB document?

A) _index
B) _name
C) _id
D) _key

Answer: Option C

Explanation: Each MongoDB document has an _id field, which uniquely identifies it.

16.) Which of the following operators is used to match multiple values in MongoDB?

A) $and
B) $or
C) $match
D) $in

Answer: Option D

Explanation: The $in operator is used to match documents where a field’s value is within an array of specified values.

17.) What is the data type of the _id field by default?

A) String
B) Integer
C) ObjectId
D) Binary

Answer: Option C

Explanation: In MongoDB, the default data type for the _id field is ObjectId. This is a 12-byte identifier that is unique to each document in a collection, and it is automatically generated by MongoDB unless you specify a different value for the _id field.

18.) Which command retrieves the total number of documents in a collection?

A) db.collection.count()
B) db.collection.total()
C) db.collection.size()
D) db.collection.numDocuments()

Answer: Option A

Explanation: The count() method returns the number of documents in a collection.

19.) Which method is used to update multiple documents in a collection?

A) db.collection.update()
B) db.collection.updateMany()
C) db.collection.modifyMany()
D) db.collection.batchUpdate()

Answer: Option B

Explanation: The updateMany() method updates all documents that match the query.

20.) Which data type is used to store an array of values in MongoDB?

A) Array
B) List
C) Set
D) Collection

Answer: Option A

Explanation: MongoDB uses the Array data type to store multiple values in a single field.

Leave a Reply

Your email address will not be published. Required fields are marked *