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

Object Constructor methods like ObjectId, UUID, Date do not work with a field parameter value with $addFields

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.6.5
    • Component/s: Aggregation Framework
    • Labels:
      None
    • ALL
    • Hide
      1. The following code example executed in a Mongo shell does not work properly
        I am trying to create the ObjectId of string field someId into field newId:

      db.collection1.aggregate([
        {$addFields: {newId: ObjectId("$someId")}},
         {$out: "collection1" }
      ])

      1. If in the above code I replace ObjectId and someId with:
        a.) UUID and a field with a UUID hex string - it ignores the hex string and instead generates a value that seems to be based on the name of the field and not the value in the field

      b) Date and a field with a date - it generates the same date value ignoring the date value in the field 

      1.  The equivalent code which works and doesn't use aggregation works:

      db.coll1.find().forEach(function(results){
        print( "fileId: " + results.fileId );
        try {
          db.coll1.update( {_id : results._id, someId: {$type: ["string"]}}, 
                                               {$set:

      {newId: ObjectId(results.someId)}

       }) 
       } catch (err)

      {     print(err.name + ': "' + err.message + '" occurred using someId '+ results.someId +".");   }

      })
      newId is created as a new field with the ObjectId of the someId field

      Show
      The following code example executed in a Mongo shell does not work properly I am trying to create the ObjectId of string field someId into field newId: db.collection1.aggregate([   {$addFields: {newId: ObjectId("$someId")}},    {$out: "collection1" } ]) If in the above code I replace ObjectId and someId with: a.) UUID and a field with a UUID hex string - it ignores the hex string and instead generates a value that seems to be based on the name of the field and not the value in the field b) Date and a field with a date - it generates the same date value ignoring the date value in the field   The equivalent code which works and doesn't use aggregation works: db.coll1.find().forEach(function(results){   print( "fileId: " + results.fileId );   try {     db.coll1.update( {_id : results._id, someId: {$type: ["string"] }},                                           {$set: {newId: ObjectId(results.someId)}  })   } catch (err) {     print(err.name + ': "' + err.message + '" occurred using someId '+ results.someId +".");   } }) newId is created as a new field with the ObjectId of the someId field

      1. I want to update the equivalent of a foreign key field that has an ObjectId string value with the ObjectId itself. I have tried using the aggregation $addFields and have discovered that ObjectId("$hexStringField") errors with:

      Failed to execute script.

      Error: invalid object id: length :
      @(shell):2:26

      1. I suspected that there might be a problem using Object constructor methods so I tried using $addFields with UUID("$uuidStringField") and Date("$dateField").
        Using these methods did not produce an error, but did not give the expected results.

       

            Assignee:
            nick.brewer Nick Brewer
            Reporter:
            ensefik Edem Nsefik
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: