Using validation for a collection with following document validator:
db.createCollection("col1", { validator:{ "$expr": { $lt: ["$ts", "$$NOW"] } } });
results with
{ ok: 1 }
An attempt to insert a document with any value in `ts` field fail validation with following error:
{ result:
{ ok: 1,
writeErrors:
[ { err:
{ index: 0,
code: 121,
errmsg: 'Document failed validation',
errInfo:
{ failingDocumentId: {},
details:
{ operatorName: '$expr',
specifiedAs: { '$expr': { '$lt': [ '$ts', '$$NOW' ] } },
reason: 'failed to evaluate aggregation expression',
details:
{ code: 51144,
codeName: 'Location51144',
errmsg: 'Builtin variable \'$$NOW\' is not available' } } },
op: { ts: 2020-05-16T08:28:14.938Z, _id: {} } } } ],
writeConcernErrors: [],
insertedIds: [ { index: 0, _id: {} } ],
nInserted: 0,
nUpserted: 0,
nMatched: 0,
nModified: 0,
nRemoved: 0,
upserted: [] } }