[SERVER-35739] Object Constructor methods like ObjectId, UUID, Date do not work with a field parameter value with $addFields Created: 22/Jun/18  Updated: 14/Aug/18  Resolved: 17/Jul/18

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 3.6.5
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Edem Nsefik Assignee: Nick Brewer
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:
  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

Participants:

 Description   
  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.

 



 Comments   
Comment by Nick Brewer [ 17/Jul/18 ]

ensefik

This can be accomplished with the new type conversion operators introduced in MongoDB 4.0.

Nick

Comment by Nick Brewer [ 22/Jun/18 ]

ensefik

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

Can you provide an example of this behavior?

Using these methods did not produce an error, but did not give the expected results.

What results did you receive?

In general, it would be useful to get the specific commands you're running (including UUIDs, dates, etc), as well as the output you're receiving.

Thanks,
Nick

Generated at Thu Feb 08 04:40:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.