Inserting New Object Id

Hello Everyone,

I was going through the Basic Course M001 In chapter 3 - Creating and Manioulating Documents

Section 1 - * [Lecture: Inserting New Documents - ObjectId]
Have mentioned few values such as certificate number, id name etc how can we make sure that those values were correct and what values to be used.

If someone guide would be great.

Before posting this I looking through earlier discussion couldn’t find the relevant one

Hi @Kamal_Setia, welcome to the community :wave: .
If you want to ensure that the documents that are getting inserted and updated follow a particular structure(data type and value ranges), you can consider implementing Schema Validation.

By specifying the JSON Schema for a particular collection, you can implement validation rules in a human-readable format.

db.createCollection("certificates", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         title: "Certificate Validation",
         required: [ "certificationNumber", "name", "id"],
         properties: {
            certificationNumber: {
               bsonType: "int",
               minimum: 10000,
               maximum: 100000,
               description: "'Certification Number must be in the range of 10000 to 100000"
            },
            name: {
               bsonType: "string",
               description: "name must be a string"
            },
            id: {
               bsonType: "int",
               minimum: 1,
               description: "'id must be greater than 0"
            }
         }
      }
   }
} )

Learn about the available keywords for JSON schema.

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.