liveBook · Manning

$validate

As explained in MongoDB 8.0 in Action, Third Edition

$validate is an Atlas Stream Processing stage that checks incoming documents against specified schema conditions and controls how invalid documents are handled.

$validate checks each incoming document in an Atlas Stream Processing pipeline against a user-defined schema. Use it when downstream processing or storage requires particular fields, data types, values, or ranges. Documents that fail validation do not proceed to the next stage. section 15.5.2 section 15.6.4

{
  "$validate": {
    "validator": { <filter> },  #1
    "validationAction": "discard" | "dlq"  #2
  }
}

The stage has two settings: validator holds the conditions, and validationAction determines what happens when a document fails them. section 15.5.2

To add validation to a processor:

  • Start the pipeline with $source, then place $validate anywhere before the final $emit or $merge stage. section 15.5.2 section 15.5
  • Put the required field checks in validator. It supports query operators except $near, $nearSphere, $text, and $where. section 15.5.2
  • Choose validationAction: discard drops invalid documents and is the default; dlq logs them to a dead-letter queue while discarding them, without guarantees. section 15.5.2

Validate records before storing them

A processor can read from a MongoDB collection, validate that name exists and is a string, age exists and is an integer of at least 18, and email matches a pattern, then send passing documents to a destination collection. With validationAction set to discard, failing records never reach that destination. section 15.6.4

$validate is optional. Add it when rejecting malformed stream data is preferable to letting that data reach later transformations or a sink. If failed documents need further handling, consider dlq, but do not treat its logging as guaranteed delivery. section 15.5 section 15.5.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines configuration, operator exclusions, placement, failure actions, and a source-to-sink assembly; stops short of delivery guarantees for dlq.
Examples
Uses a MongoDB source and destination with name, adult-age, and email checks, plus a validation prototype.
What it assumes
Builds a processor from an empty array in mongosh, adding a source before validation and a sink afterward.
What you can do after
Apply it via Atlas Stream Processing to reject nonconforming documents and select discard or dlq handling.
How it connects
Connects schema validation to $source, aggregation pipelines, $emit, $merge, and dead-letter queues.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.