Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-73125

Builtin variable $$NOW is not available in document validation

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.0-rc0, 6.3.0-rc1, 5.0.16, 6.0.6
    • Affects Version/s: 5.0.8
    • Component/s: None
    • Labels:
      None
    • Query Execution
    • Fully Compatible
    • ALL
    • v6.3, v6.0, v5.0
    • Hide
      db.col1.drop();
      db.createCollection("col1", {validator:{"$expr": {$lt: ["$ts", "$$NOW"]}}}); 
      db.col1.insert({"ts": new Date(1589617694938)});

       

      Show
      db.col1.drop(); db.createCollection( "col1" , {validator:{ "$expr" : {$lt: [ "$ts" , "$$NOW" ]}}}); db.col1.insert({ "ts" : new Date(1589617694938)});  
    • QE 2023-02-20, QE 2023-03-06

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

       

       

            Assignee:
            naama.bareket@mongodb.com Naama Bareket
            Reporter:
            ukrtelecom@gmail.com Alex Blex
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: