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

$dateFromString behavior differs from Date()/ISODate()

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 4.2.8
    • Component/s: Aggregation Framework
    • Labels:
    • Environment:
      macOS 10.14.6
    • Query Execution
    • ALL
    • Hide

      > use test

      switched to db test

      > version()

      4.2.8

      > db.test.find({})

      {{

      { "_id" : ObjectId("5efbaba4250838017f12c045"), "dateString" : "2006-01-02T15:04:05.123456789Z" }

      }}

      > db.test.aggregate([\{$project: {date: {$dateFromString: {dateString: '$dateString'}}}}])

      2020-06-30T14:22:04.013-0700 E  QUERY    [js] uncaught exception: Error: command failed: {

      "ok" : 0,

      "errmsg" : "Error parsing date string '2006-01-02T15:04:05.123456789Z'; 0: passing a time zone identifier as part of the string is not allowed '2'",

      "code" : 241,

      "codeName" : "ConversionFailure"

      } : aggregate failed :

      _getErrorWithCode@src/mongo/shell/utils.js:25:13

      doassert@src/mongo/shell/assert.js:18:14

      _assertCommandWorked@src/mongo/shell/assert.js:583:17

      assert.commandWorked@src/mongo/shell/assert.js:673:16

      DB.prototype._runAggregate@src/mongo/shell/db.js:266:5

      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1012:12

      @(shell):1:1

      > db.test.aggregate([{$project: {date: {$dateFromString: {dateString: {$substrBytes: ['$dateString', 0, 28]}}}])}}

      {{

      { "_id" : ObjectId("5efbaba4250838017f12c045"), "date" : ISODate("2006-01-02T15:04:05.123Z") }

      }}

      > db.test.aggregate([{$project: {date: {$dateFromString: {dateString: {$substrBytes: ['$dateString', 0, 29]}}}])}}

      2020-06-30T14:21:22.393-0700 E  QUERY    [js] uncaught exception: Error: command failed: {

      "ok" : 0,

      "errmsg" : "Error parsing date string '2006-01-02T15:04:05.123456789'; 0: passing a time zone identifier as part of the string is not allowed '2'",

      "code" : 241,

      "codeName" : "ConversionFailure"

      } : aggregate failed :

      _getErrorWithCode@src/mongo/shell/utils.js:25:13

      doassert@src/mongo/shell/assert.js:18:14

      _assertCommandWorked@src/mongo/shell/assert.js:583:17

      assert.commandWorked@src/mongo/shell/assert.js:673:16

      DB.prototype._runAggregate@src/mongo/shell/db.js:266:5

      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1012:12

      @(shell):1:1

      > ISODate('2006-01-02T15:04:05.123456789Z')

      ISODate("2006-01-02T15:04:05.123Z")

      > Date('2006-01-02T15:04:05.123456789Z')

      Tue Jun 30 2020 14:23:41 GMT-0700 (PDT)

      > db.test.updateOne({_id: ObjectId('5efbaba4250838017f12c045')}, {$set: {'dateString': '2006-01-02T15:04:05.12345678Z')}}
      {{

      { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

      }}
      > db.test.aggregate([\{$project: {date: {$dateFromString: {dateString: '$dateString'}}}}])
      {{

      { "_id" : ObjectId("5efbaba4250838017f12c045"), "date" : ISODate("2006-01-02T15:04:05.123Z") }

      }}
      > db.test.updateOne({_id: ObjectId('5efbaba4250838017f12c045')}, {$set: {'dateString': '2006-01-02T15:04:05.123456789Z')}}
      {{

      { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

      }}
      > db.test.aggregate([\{$project: {date: {$dateFromString: {dateString: '$dateString'}}}}])
      2020-06-30T14:37:05.058-0700 E QUERY [js] uncaught exception: Error: command failed: {
      {{ "ok" : 0,}}
      {{ "errmsg" : "Error parsing date string '2006-01-02T15:04:05.123456789Z'; 0: passing a time zone identifier as part of the string is not allowed '2'",}}
      {{ "code" : 241,}}
      {{ "codeName" : "ConversionFailure"}}
      } : aggregate failed :
      _getErrorWithCode@src/mongo/shell/utils.js:25:13
      doassert@src/mongo/shell/assert.js:18:14
      _assertCommandWorked@src/mongo/shell/assert.js:583:17
      assert.commandWorked@src/mongo/shell/assert.js:673:16
      DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1012:12
      @(shell):1:1

      >

       

      Show
      > use test switched to db test > version() 4.2.8 > db.test.find({}) {{ { "_id" : ObjectId("5efbaba4250838017f12c045"), "dateString" : "2006-01-02T15:04:05.123456789Z" } }} > db.test.aggregate( [\{$project: {date: {$dateFromString: {dateString: '$dateString'}}}}] ) 2020-06-30T14:22:04.013-0700 E  QUERY    [js] uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "Error parsing date string '2006-01-02T15:04:05.123456789Z'; 0: passing a time zone identifier as part of the string is not allowed '2'", "code" : 241, "codeName" : "ConversionFailure" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:583:17 assert.commandWorked@src/mongo/shell/assert.js:673:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1012:12 @(shell):1:1 > db.test.aggregate([{$project: {date: {$dateFromString: {dateString: {$substrBytes: ['$dateString', 0, 28] }}}])}} {{ { "_id" : ObjectId("5efbaba4250838017f12c045"), "date" : ISODate("2006-01-02T15:04:05.123Z") } }} > db.test.aggregate([{$project: {date: {$dateFromString: {dateString: {$substrBytes: ['$dateString', 0, 29] }}}])}} 2020-06-30T14:21:22.393-0700 E  QUERY    [js] uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "Error parsing date string '2006-01-02T15:04:05.123456789'; 0: passing a time zone identifier as part of the string is not allowed '2'", "code" : 241, "codeName" : "ConversionFailure" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:583:17 assert.commandWorked@src/mongo/shell/assert.js:673:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1012:12 @(shell):1:1 > ISODate('2006-01-02T15:04:05.123456789Z') ISODate("2006-01-02T15:04:05.123Z") > Date('2006-01-02T15:04:05.123456789Z') Tue Jun 30 2020 14:23:41 GMT-0700 (PDT) > db.test.updateOne({_id: ObjectId('5efbaba4250838017f12c045')}, {$set: {'dateString': '2006-01-02T15:04:05.12345678Z' )}} {{ { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } }} > db.test.aggregate( [\{$project: {date: {$dateFromString: {dateString: '$dateString'}}}}] ) {{ { "_id" : ObjectId("5efbaba4250838017f12c045"), "date" : ISODate("2006-01-02T15:04:05.123Z") } }} > db.test.updateOne({_id: ObjectId('5efbaba4250838017f12c045')}, {$set: {'dateString': '2006-01-02T15:04:05.123456789Z' )}} {{ { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } }} > db.test.aggregate( [\{$project: {date: {$dateFromString: {dateString: '$dateString'}}}}] ) 2020-06-30T14:37:05.058-0700 E QUERY [js] uncaught exception: Error: command failed: { {{ "ok" : 0,}} {{ "errmsg" : "Error parsing date string '2006-01-02T15:04:05.123456789Z'; 0: passing a time zone identifier as part of the string is not allowed '2'",}} {{ "code" : 241,}} {{ "codeName" : "ConversionFailure"}} } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:583:17 assert.commandWorked@src/mongo/shell/assert.js:673:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1012:12 @(shell):1:1 >  
    • Query 2020-08-24, Query 2020-09-07, Query 2020-09-21

      I have a document with a nanosecond precision ISO timestamp in a string ("0000-00-00T00:00:00.000000000Z"). I do not control the schema or formatting of this document.

      When I try to use $dateFromString or $toDate on this field from an aggregation, I get the following error:

      Error parsing date string '2006-01-02T15:04:05.123456789Z'; 0: passing a time zone identifier as part of the string is not allowed '2'

      If I trim off the nanosecond digit (to 10 ns precision or 8 decimal places), it seems to handle it okay. Additionally, the ISODate() and Date() constructors are able to parse the string just fine.

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            mmeng@venteclife.com Marshall Meng
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: