[SERVER-73125] Builtin variable $$NOW is not available in document validation Created: 20/Jan/23  Updated: 29/Oct/23  Resolved: 27/Feb/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 5.0.8
Fix Version/s: 7.0.0-rc0, 6.3.0-rc1, 5.0.16, 6.0.6

Type: Bug Priority: Major - P3
Reporter: Alex Blex Assignee: Naama Bareket
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Assigned Teams:
Query Execution
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v6.3, v6.0, v5.0
Steps To Reproduce:

db.col1.drop();
db.createCollection("col1", {validator:{"$expr": {$lt: ["$ts", "$$NOW"]}}}); 
db.col1.insert({"ts": new Date(1589617694938)});

 

Sprint: QE 2023-02-20, QE 2023-03-06
Participants:

 Description   

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: [] } } 

 

 



 Comments   
Comment by Githook User [ 15/Mar/23 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-73125: Set built-in variables on ExpressionContext of the validator

(cherry-picked from 7820a2ce672fffb91b9f03bd72c1cf827efc391e)
Branch: v6.0
https://github.com/mongodb/mongo/commit/1cb3977eeeed7bfc5024f481af8d17f8ae78241c

Comment by Githook User [ 28/Feb/23 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-73125: Set built-in variables on ExpressionContext of the validator
(cherry-picked from 7820a2ce672fffb91b9f03bd72c1cf827efc391e)
Branch: v6.3
https://github.com/mongodb/mongo/commit/c7bd639a41b7001046be2a70d70ea92753912580

Comment by Githook User [ 28/Feb/23 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-73125: Set built-in variables on ExpressionContext of the validator
(cherry-picked from 7820a2ce672fffb91b9f03bd72c1cf827efc391e)
Branch: v5.0
https://github.com/mongodb/mongo/commit/fedef84731a58b7226789675620cccd4cc42db94

Comment by Githook User [ 25/Feb/23 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-73125: Set built-in variables on ExpressionContext of the validator
Branch: master
https://github.com/mongodb/mongo/commit/7820a2ce672fffb91b9f03bd72c1cf827efc391e

Generated at Thu Feb 08 06:23:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.