How to find all invalid document based on jsonSchema validator? One of the new feature in MongoDB 3.6 is jsonSchema validator. I can create a collection with an validator like:
{
  &quot;validator&quot;: {
    &quot;$jsonSchema&quot;: {
      &quot;properties&quot;: {
        &quot;name&quot;: {
          &quot;bsonType&quot;: &quot;string&quot;,
          &quot;description&quot;: &quot;name&quot;
        },
        &quot;age&quot;: {
          &quot;bsonType&quot;: &quot;int&quot;,
          &quot;minimum&quot;: 10,
          &quot;maximum&quot;: 30
        }
      },
      &quot;required&quot;: [&quot;name&quot;, &quot;age&quot;],
      &quot;bsonType&quot;: &quot;object&quot;
    }
  }
}

But what if the collection already existed with some documents, that don't fullfill the validation schema? How can I found out all invalid documents based on the validator?
You can use the $jsonSchema query operator to find relevant documents.
For example, using the mongo shell:
// Retrieve the current validator for the &quot;students&quot; collection
var schema = db.getCollectionInfos({ name: &quot;students&quot; })[0].options.validator

// Find any documents not matching the schema
db.students.find({ $nor: [schema] })

Similarly, you can use the $jsonSchema query operator to confirm expected matches before adding or updating the validator for a collection:
var schema = {
  $jsonSchema: {
    properties: {
      name: {
        bsonType: &quot;string&quot;,
        description: &quot;name&quot;,
      },
    },
    required: [&quot;name&quot;],
    bsonType: &quot;object&quot;,
  },
}

// Find documents matching schema
db.students.find(schema)