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?
2.) Where is data validation implemented in MongoDB?
3.) Which command is used to create a collection with validation rules?
4.) What MongoDB operator is commonly used in validation rules to specify field requirements?
5.) Which validation action in MongoDB allows documents that fail validation but logs a warning?
6.) What does the required keyword do in a JSON schema?
7.) Which of the following is true about the additionalProperties keyword in a JSON schema?
8.) What is the default validation level in MongoDB?
9.) Which validation level allows invalid documents to exist but prevents new invalid documents from being added?
10.) How can you update validation rules for an existing collection?