Robert C. Holland
Robert C. Holland

Reputation: 1813

Debugging Document Validation Error

From mongo shell: db.getCollectionInfos({ name : 'applications' }) returns:

[
    {
        "name" : "applications",
        "type" : "collection",
        "options" : {
            "validator" : {
                "$and" : [
                    {
                        "user_name" : {
                            "$type" : "string"
                        }
                    },
                    {
                        "password" : {
                            "$type" : "string"
                        }
                    },
                    {
                        "role" : {
                            "$type" : "bool"
                        }
                    },
                    {
                        "deposit_amount" : {
                            "$type" : "double"
                        }
                    },
                    {
                        "submit_date" : {
                            "$type" : "int"
                        }
                    },
                    {
                        "user_ip" : {
                            "$type" : "string"
                        }
                    }
                ]
            },
            "validationLevel" : "strict",
            "validationAction" : "error"
        },
        "info" : {
            "readOnly" : false
        },
        "idIndex" : {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_",
            "ns" : "turk_system.applications"
        }
    }
]

In node.js, attempting to use the official mongodb driver to insert a document:

//...
db.collection('applications').insertOne
(
    {
        'user_name' : 'test123',
        'password' : 'test123',
        'role' : true,
        'deposit_amount' : Number.parseFloat('34.5'),
        'submit_date' : mongodb.Long.fromNumber(500),
        'user_ip' : '192.168.100.1'
    }
)
.then((result) =>
{
    //...
})
.catch((err) =>
{
    console.log(err);
});

Catches error:

{ MongoError: Document failed validation
    at Function.MongoError.create (/loc/to/node_modules/mongodb-core/lib/error.js:31:11)
    at toError (/loc/to/node_modules/mongodb/lib/utils.js:139:22)
    at /loc/to/node_modules/mongodb/lib/collection.js:748:67
    at /loc/to/node_modules/mongodb-core/lib/connection/pool.js:469:18
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoError',
message: 'Document failed validation',
driver: true,
index: 0,
code: 121,
errmsg: 'Document failed validation' }

Mongodb document validation error isn't very descriptive, but my guess is that there is some mismatch between how mongo and node.js represents types, but I am not sure where.

How may I fix above issue?

(mongodb version : v3.4.4, on linux)

Upvotes: 2

Views: 900

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151132

No it's not very descriptive and that's basically all you get. This is a very new feature so the only real "debugging" you get is by looking at the client code you are sending. It fails because Long returns a 64-bit integer "Defines a Long class for representing a 64-bit two's-complement", and your constraint is looking for a 32-bit integer. It should be "long" instead of "int". See $type

        {
            "submit_date" : {
                "$type" : "long"
            }
        },

Alternately you use .toInt() to return a 32-bit integer:

{
    'user_name' : 'test123',
    'password' : 'test123',
    'role' : true,
    'deposit_amount' : Number.parseFloat('34.5'),
    'submit_date' : mongodb.Long.fromNumber(500).toInt(),
    'user_ip' : '192.168.100.1'
}

As long as it is a value that can actually be represented as a 32-bit integer that is.

If you are looking for "schema" then you "should" instead be looking at client side implementations instead. Document Validation is not intended to be a replacement for that sort of functionality, and you will get more detailed error messages from "client side" submissions that you presently will from the server.

Upvotes: 2

Related Questions