Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-242

MongoDB update JsonValidator with NodeJS keeps failing

      Json validation for my db update/upsert operations keeps failing. Updates are only successful when I remove the db.createCollection validator option parameter. But schema validation is crucial for this use-case so development is currently at a halt and 3 days have been lost as a result. 
       
      Schema

       {
          additionalProperties: false,
          bsonType: 'object',
          description: 'email data point',
          properties: {
              _id: {
                  bsonType: 'objectId'
              },
              address: {
                  bsonType: 'string',
                  maxLength: 64,
                  pattern: '^\w+@\w+(.\w+)+$',
                  description: 'required: email address'
              },
              contactTypes: {
                  bsonType: 'array',
                  description: 'required: a set of unique string values',
                  items: {
                      bsonType: 'string',
                      enum: email.map( e => e.value )
                  },
                  uniqueItems: true
              },
              dateCreated: {
                  bsonType: 'long',
                  description: 'required: contact email creation timestamp in ms since epoch'
             },
              isPrimary: {
                  bsonType: 'bool'
             },
              lastUpdated: {
                  bsonType: 'long',
                  description: 'contact email recent update timestamp in ms since epoch'
              }
          },
          required: [ 'address', 'contactTypes', 'dateCreated', 'isPrimary' ]
      }{{}}
      

       
      Shell call:
       MongoDB Enterprise D0-shard-0:PRIMARY> db.emails.findOneAndUpdate({
      address: 'adfadfslkds@ffk.co.uk',
      contactTypes: [ 'p', 'b' ],
      isPrimary: true
      }, {
      $set:

      { address: 'adfadfslkds@ffk.co.uk', contactTypes: [ 'p', 'b' ], isPrimary: true, dateCreated: 1591981465 }

      }, {
      returnNewDocument: true,
      upsert: true
      })
       
      Result:
       2020-06-12T12:33:47.785-0700 E QUERY [js] uncaught exception: Error: findAndModifyFailed failed: {
      "operationTime" : Timestamp(1591990428, 24),
      "ok" : 0,
      "errmsg" : "Document failed validation",
      "code" : 121,
      "codeName" : "DocumentValidationFailure",
      "$clusterTime" : {
      "clusterTime" : Timestamp(1591990428, 24),
      "signature" :

      { "hash" : BinData(0,"9QzeeWQw4kYtnf1m/2dTyvNphAQ="), "keyId" : NumberLong("6798592801870184449") }

      }
      } :
      _getErrorWithCode@src/mongo/shell/utils.js:25:13
      DBCollection.prototype.findAndModify@src/mongo/shell/collection.js:725:15
      DBCollection.prototype.findOneAndUpdate@src/mongo/shell/crud_api.js:900:12
      @(shell):1:1
       
      Thanks in advance for looking into this issue. 

            Assignee:
            Unassigned Unassigned
            Reporter:
            stephen.isienyi@gmail.com Stephen Isienyi
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: