anita shrivastava
anita shrivastava

Reputation: 329

Mongodb Document failed validation even when inserting correct data

I have written the validation for one of my collection. I am inserting document in mongodb atlas accordingly but still its is saying validation error.

Validation

{
  $jsonSchema: {
    bsonType: 'object',
    additionalProperties: false,
    required: [
      'userName'
    ],
    properties: {
      userName: {
        bsonType: 'string',
        description: 'asdasdasdasdasdasdasd'
      }
    }
  }
}

Insert

/** * Paste one or more documents here*/
{
    "_id": {
        "$oid": "605e144151348bafd4b9c0e2"
    },
    "userName":"asdasdasd"    
}

Upvotes: 0

Views: 678

Answers (1)

Joe
Joe

Reputation: 28356

The defined schema permits only the namespace field, and disallows any other fields.

MongoDB cannot store a document without a field named _id, since that is not permitted by the validation, no document can be inserted.

Upvotes: 1

Related Questions