{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "subdocument": {
            "type": "object",
            "patternProperties": {
                "^[^$]": {
                    "$ref": "#/definitions/value"
                }
            },
            "additionalProperties": false
        },
        "value": {
            "oneOf": [
                {
                    "type": "string"
                },
                {
                    "type": "number"
                },
                {
                    "type": "boolean"
                },
                {
                    "type": "null"
                },
                {
                    "$ref": "#/definitions/$binary"
                },
                {
                    "$ref": "#/definitions/$date"
                },
                {
                    "$ref": "#/definitions/$oid"
                },
                {
                    "$ref": "#/definitions/$timestamp"
                },
                {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/value"
                    }
                },
                {
                    "$ref": "#/definitions/subdocument"
                }
            ]
        },
        "$binary": {
            "type": "object",
            "properties": {
                "$binary": {
                    "type": "string"
                },
                "$type": {
                    "type": "string",
                    "pattern": "[a-z0-9]"
                }
            },
            "required": [
                "$binary",
                "$type"
            ],
            "additionalProperties": false
        },
        "$date": {
            "type": "object",
            "properties": {
                "$date": {
                    "type": "integer"
                }
            },
            "required": [
                "$date"
            ],
            "additionalProperties": false
        },
        "$oid": {
            "type": "object",
            "properties": {
                "$oid": {
                    "type": "string",
                    "pattern": "^[0-9a-f]{24}$"
                }
            },
            "required": [
                "$oid"
            ],
            "additionalProperties": false
        },
        "$timestamp": {
            "type": "object",
            "properties": {
                "$timestamp": {
                    "type": "object",
                    "properties": {
                        "t": {
                            "type": "integer"
                        },
                        "i": {
                            "type": "integer"
                        }
                    },
                    "required": [
                        "t",
                        "i"
                    ],
                    "additionalProperties": false
                }
            },
            "required": [
                "$timestamp"
            ],
            "additionalProperties": false
        }
    },
    "allOf": [
        {
            "$ref": "#/definitions/subdocument"
        },
        {
            "required": [
                "_id"
            ]
        }
    ]
}