MongoDB MCQs – Data Validation

Data validation in MongoDB ensures that documents conform to predefined rules, maintaining data integrity and consistency. By using JSON schema, MongoDB allows developers to define constraints such as required fields, data types, string patterns, and numeric ranges. Validation rules can be applied at the collection level with different levels (strict, moderate) and actions (error, warn). These MCQs cover the key concepts that are often tested in interviews.

1.) What is the primary purpose of data validation in MongoDB?

A) To enforce relationships between documents
B) To ensure that inserted or updated documents meet specific criteria
C) To improve query performance
D) To manage indexes

Answer: Option B

Explanation: Data validation ensures documents conform to a predefined schema, maintaining data integrity and consistency.

2.) Where is data validation implemented in MongoDB?

A) At the collection level
B) At the database level
C) At the field level
D) At the server level

Answer: Option A

Explanation: Data validation is configured at the collection level using validation rules.

3.) Which command is used to create a collection with validation rules?

A) db.createCollection()
B) db.collection.setValidation()
C) db.collection.addValidationRules()
D) db.newCollection()

Answer: Option A

Explanation: The db.createCollection() method allows specifying validation rules during collection creation.

4.) What MongoDB operator is commonly used in validation rules to specify field requirements?

A) $match
B) $group
C) $validate
D) $jsonSchema

Answer: Option D

Explanation: The $jsonSchema operator defines validation rules for a collection, specifying field types, required fields, and constraints.

5.) Which validation action in MongoDB allows documents that fail validation but logs a warning?

A) error
B) error
C) skip
D) ignore

Answer: Option B

Explanation: The warn validation action permits invalid documents to be inserted but logs a warning message.

6.) What does the required keyword do in a JSON schema?

A) Ensures a field is indexed
B) Sets default values for a field
C) Specifies that a field must be present in a document
D) Prevents updates to a field

Answer: Option C

Explanation: The required keyword enforces the presence of specific fields in a document.

7.) Which of the following is true about the additionalProperties keyword in a JSON schema?

A) It allows only predefined fields in a document
B) It defines default values for fields
C) It validates array elements
D) It allows the inclusion of extra fields

Answer: Option A

Explanation: The additionalProperties: false setting restricts documents to only the fields defined in the schema.

8.) What is the default validation level in MongoDB?

A) strict
B) moderate
C) off
D) strictValidation

Answer: Option A

Explanation: The strict validation level ensures that all documents, whether inserted or updated, must conform to the validation rules.

9.) Which validation level allows invalid documents to exist but prevents new invalid documents from being added?

A) strict
B) moderate
C) off
D) loose

Answer: Option B

Explanation: The moderate validation level permits existing invalid documents but enforces validation rules for new or updated documents.

10.) How can you update validation rules for an existing collection?

A) db.collection.updateValidation()
B) db.collection.modifyRules()
C) db.runCommand({ collMod })
D) db.collection.resetValidation()

Answer: Option C

Explanation: The collMod command is used to modify validation rules for an existing collection.

Leave a Reply

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