MongoServerError: Document failed validation

Hi,
According to Schema validation, I have a problem.
I can’t see more details about the error when working on the cluster:
I see that when i connect mongosh to the cluster and insert a document the collection, i only receive as error message :
**MongoServerError**: Document failed validation

But when i connect to local i receive additionnal informations :

MongoServerError: Document failed validation
Additional information: {
failingDocumentId: ObjectId(“610b39744caa3a009907a5f0”),
details: {
operatorName: ‘$and’,
clausesNotSatisfied: [
{
index: 0,
details: {
operatorName: ‘$and’,
clausesNotSatisfied: [
{ index: 0, details: [Object] },
{ index: 3, details: [Object] },
{ index: 4, details: [Object] },
{ index: 6, details: [Object] }
]
}
}
]
}
}

We cannot help if you do not share the schema together with the document that fails.

1 Like
use("school");
db.createCollection("persons",{
    validator: {
        $jsonSchema: {
            bsonType: "object",
            required: [ "first_name", "last_name", "gender" ],
            properties: {
                first_name: { 
                    bsonType: "string" 
                    },
                last_name: { 
                    bsonType: "string" 
                    },
                gender: { 
                    bsonType: "string" 
                    },
                category: { 
                    enum: [ "student", "proffessor", "supervisor", null] 
                    },
                phone: { 
                    bsonType: "string"
                    },
                address: { 
                    bsonType: "object",
                    properties: {
                        street_number: { 
                            bsonType: "int" 
                            },
                        street_name: { 
                            bsonType: "string" 
                            },
                        house_number: { 
                            bsonType: "int" 
                            },
                        }
                }
            }
        }
    },
});




use("school");
    db.persons.insertOne({
        first_name: "August",
        last_name: "Marline",
        gender: 2,
        category: "proffessor",
        phone: "581",
        address: {
            street_number: 345,
            street_name: "Auclair",
            house_number: 67
        }
    });

So easy, I do not know how you could not set it yourself.

and

And to be clear 2 is a number, not a string.

Hi @steevej ,

Thank you, I know. Thanks for reply.

As i told in my first post, i’m connected in a cluster on atlass.

For the insertOne query, i puted 2 in the gender in order to see if the validator will show a error message according to the information that i inserted. So, it’s okey, I had this error message :
MongoServerError: Document failed validation
But, mongosh does not retrun additional information in order to see where at what fileld is the error.

1 Like

Hi,

I’m trying the same thing (add a validation schema, insert one document that does not pass the validation), I get the same behavior (document failed validation, but not the promised detailed info at least firing the query from the console or mongo compass console), I have even tried to upgrade to 5.0.2 version, it seems that this feature is not 100% ready or am I missing something?

2 Likes