Can you get good error messages for schema validaiton, on existing data?

Hi there.
My objective is to upgrade an existing mongo database to enforce schemas. Reading this blog, it seems like version 5 actually gives good error messages on insert/update.

But how do I get these error messages for existing data?

This section from compass is to no help (for complex objects that is):

Hi @Alex_Bjorlig

You can validate existing data using db.collection.validate():

db.collection.validate() also validates any documents that violate the collection’s schema validation rules.

Hope this helps.

Best regards

1 Like

I don’t seem to get schema validation erros as output, just something like this:

"validateOutput": {
        "ns": "21risk.sessions",
        "nInvalidDocuments": 0,
        "nrecords": 181856,
        "nIndexes": 2,
        "keysPerIndex": {
            "_id_": 181856,
            "expires_1": 181856
        "indexDetails": {
            "_id_": {
                "valid": true
            "expires_1": {
                "valid": true
        "valid": true,
        "repaired": false,
        "warnings": [],
        "errors": [],
        "extraIndexEntries": [],
        "missingIndexEntries": [],
        "corruptRecords": [],
        "ok": 1,
        "$clusterTime": {
            "clusterTime": {
                "$timestamp": "7125046445631078401"
            "signature": {
                "hash": "Kq8B4EiRRx5Q7A7Wk1faUpcRfUk=",
                "keyId": {
                    "low": 2,
                    "high": 1646856462,
                    "unsigned": false
        "operationTime": {
            "$timestamp": "7125046445631078401"

With the node.js driver, I call the validate like this validateOutput = await db.admin().validateCollection(;

But I don’t seem to have an option for setting full: true - as described in the docs here.


Currently I simply validate the schema with ajv:

import Ajv from 'ajv';
const ajv = new Ajv({ strict: false });
const validate = ajv.compile(validator.$jsonSchema);
  const result = validate(failedDoc);
  if (!result) {
    docSchemaErrors = validate.errors;

@Stennie_X Just tagging you, because I can see in my forum searches that you seem to be the schema validation expert :pray: And my AJV workaround does not seem to work well in edge cases

For now I actually found it that the most pragmatic approach is simply to create a temporary collection, enforce schema validation, and then try to insert the document giving errors.
Works surprisingly well, and gives more stable results than using ajv.

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