MongoDB MCQs – MongoDB Basics

21.) What is the purpose of the $set operator in MongoDB?

A) To replace a document
B) To remove fields from a document
C) To update specific fields
D) To create a collection

Answer: Option C

Explanation: The $set operator is used to update specific fields in a document without replacing the entire document.

22.) How do you query documents where a field is not equal to a specific value?

A) $not
B) $ne
C) $neq
D) $notEq

Answer: Option B

Explanation: The $ne operator matches documents where a field’s value is not equal to the specified value.

23.) Which of the following is true about MongoDB collections?

A) They require a fixed schema.
B) Fields must be consistent across all documents.
C) They are schema-less.
D) They do not support indexing.

Answer: Option C

Explanation: MongoDB collections do not require a fixed schema, providing flexibility in document structure.

24.) Which command removes all documents from a collection?

A) db.collection.drop()
B) db.collection.deleteMany({})
C) db.collection.removeAll()
D) db.collection.delete()

Answer: Option B

Explanation: The deleteMany({}) method removes all documents from a collection.

25.) What is the purpose of the $unset operator?

A) To add new fields
B) To remove fields from a document
C) To delete a document
D) To replace a collection

Answer: Option B

Explanation: The $unset operator removes specified fields from a document.

26.) How can you check the structure of a collection’s documents?

A) db.collection.show()
B) db.collection.explain()
C) db.collection.findOne()
D) db.collection.structure()

Answer: Option C

Explanation: The findOne() method retrieves a single document, which can be used to check its structure.

27.) Which operator checks if a field exists in a document?

A) $exists
B) $field
C) $match
D) $include

Answer: Option A

Explanation: The $exists operator checks whether a field is present in a document.

28.) What is the purpose of the $push operator?

A) Add a field
B) Append a value to an array
C) Update multiple documents
D) Delete an array

Answer: Option B

Explanation: The $push operator appends a value to an array in a document.

29.) Which of the following methods creates a new collection?

A) db.createCollection()
B) db.collection.new()
C) db.newCollection()
D) db.collection.insert()

Answer: Option A

Explanation: The createCollection() method explicitly creates a new collection.

30.) Which MongoDB function ensures unique field values across documents?

A) $unique
B) createIndex({field: 1}, {unique: true})
C) db.collection.setUnique()
D) db.collection.addConstraint()

Answer: Option B

Explanation: The createIndex() method with {unique: true} ensures unique values for a specified field.

Leave a Reply

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