Setting default values in data schema

Hello Enthusiast,
I am implementing data schema. Here i have to mention default value to a field of document.

is there any possible way to set default value to a field?

db.createCollection("collect", {
  validator: {
        $jsonSchema: {
            bsonType: "object",
            additionalProperties: true,
            required: ["component", "path"],
            properties: {
                component: {
                    bsonType: "string"
                path: {
                   bsonType: "string",
                   description: "Set to default value"

Best Regards,
M Jagadeesh.

Hi @Manepalli_Jagadeesh,

The schema validation is only intend to enforce/validate a schema definition and not populate values.

The server does not have an ability to set default values and we recommend users to set it in their data access application layer or use ODM’s to set those values on the application side.

If you use Atlas you can consider having a trigger on the collection to update every inserted document with a specific filed value.

Best regards,


Hi @Pavel_Duchovny,

Thank you for information and providing hints to implement default values.

Best Regards,
M Jagadeesh.

Heyyo @Pavel_Duchovny,

Just wanted to confirm, this is still accurate. Collection schemas have no mechanism to provide default field values for inference on new records? Instead one can enforce defaults at the client or API level.

1 Like