1

I'm relatively new to mongo and I'm encountering a validation issue in our production environment that doesn't appear to be happening in our dev environment.

I think this is due to the validationLevel not being set to strict in dev.

I've lost hours on this now so thought I'd reach out to see if we have any mongo experts that can help me out.

Below is the jsonSchema for our collection and the document that is failing validation.

Does anyone have any idea why validation is failing. Stupidly mongo doesn't give any useful error messages.

{
"validator" : {
    "$jsonSchema" : {
        "bsonType" : "object",
        "required" : [
            "name",
            "subdomain",
            "userLimit",
            "isTrial",
            "trialExpiryDate"
        ],
        "properties" : {
            "name" : {
                "bsonType" : "string"
            },
            "subdomain" : {
                "bsonType" : "string"
            },
            "userLimit" : {
                "bsonType" : "int"
            },
            "isTrial" : {
                "bsonType" : "bool"
            },
            "trialExpiryDate" : {
                "bsonType" : "date"
            },
            "viewDisplayOrders": {
                "bsonType" : "array"
            }
        }
    }
},
"validationLevel" : "strict",
"validationAction" : "error"}

{
"_id" : ObjectId("5cc71aae77a8801149aab04d"),
"name" : "Mushroom Kingdom",
"subdomain" : "mushroom-kingdom",
"viewDisplayOrders" : [
    {
        "_id" : "5cc72902e2d2f112be40c937",
        "displayOrder" : 0
    },
    {
        "_id" : "5cc72dd5e2d2f112be40c96c",
        "displayOrder" : 1
    },
    {
        "_id" : "5cc74fbfe2d2f112be40ccba",
        "displayOrder" : 2
    },
    {
        "_id" : "5cc75658e2d2f112be40cf78",
        "displayOrder" : 3
    },
    {
        "_id" : "5cc720b148ba7011394abcbf",
        "displayOrder" : 4
    },
    {
        "_id" : "5cc8c32a5c62ac1ea3dd3d5b",
        "displayOrder" : 5
    }
],
"isTrial" : true,
"userLimit" : 10,
"trialExpiryDate" : ISODate("2019-05-28T00:00:00Z")

}

Tom Miller
  • 431
  • 1
  • 5
  • 16
  • What is the error msg even if not useful? – Eric May 19 '19 at 17:28
  • @Eric MongoError: Document failed validation at Function.create (/home/ec2-user/server/node_modules/mongodb-core/lib/error.js:43:12) at toError (/home/ec2-user/server/node_modules/mongodb/lib/utils.js:149:22) at coll.s.topology.update (/home/ec2-user/server/node_modules/mongodb/lib/operations/collection_ops.js:1465:39) at /home/ec2-user/server/node_modules/mongodb-core/lib/connection/pool.js:397:18 at process._tickCallback (internal/process/next_tick.js:61:11) – Tom Miller May 19 '19 at 18:25
  • @Eric Not sure if it's worth noting that I have updated the collection validator using db.runCommand({collMod: "col", validator: validator }); since it was initially created. – Tom Miller May 19 '19 at 18:27
  • If you add a description to your properties, does it return the description of the failing property? You could try that, like add "description":"testA", and do that for all fields (testB, testC ... ) and see if maybe you have the culprit property. – Eric May 19 '19 at 19:49
  • @Eric added a description but it didn't return it in the error message I'm afraid – Tom Miller May 19 '19 at 20:44
  • 1
    I think your error is about the date. Can you try to remove it in both your schema and the document? – Eric May 19 '19 at 22:41
  • @Eric Hi Eric, just to confirm that I removed the trialExpiryDate field from both required and properties but I'm still getting the validation error.. – Tom Miller May 20 '19 at 07:53
  • @Eric Hi it was the date field on the document. So feel free to mark this as the answer. I had set the value of the date field using db.collection.updateOne({}, {$set: {trialExpiryDate: new ISODate("2019-05-28T00:00:00/000Z") }}) – Tom Miller May 20 '19 at 16:44
  • thanks appreciate it – Eric May 20 '19 at 18:42

1 Answers1

0

The error is about the date field (ISOdate vs ISO vs "date" bsonType)

Eric
  • 9,870
  • 14
  • 66
  • 102