trying to pass the value of a field to an external function inside the $addfields operator of aggregate fails

I have been trying without success to write a script which will update all documents in a database called "locations_copy". The gist is that each document will get an additional field of type object, which will contain two subfields: "utc" of type Date and "tz" of type string. "utc" will get its' value from each document's existing "timeUTC" field, while "tz" will receive the result of a subquery from a different collection extracts the "timeZoneID" of the owner of the current location, with "subjectID" being the foreign key.

My problem is that I don't seem to be able to find a way to pass the value of the field "subjectID" to an external function. Here is my code:

db.locations_copy.aggregate( [
   { $match: {"subjectID": {"$exists": true}}},
   { $addFields: {
       time: { utc: "$timeUTC",
               tz: { "$arrayElemAt": [get_location_doc(ObjectId("$subjectID")), 0 ] }}
] ).forEach(function(x){})

with "get_location_doc" being an external function that returns an array (don't ask why) with the element being what I need.

No matter what I try, the "$subjectID" I send to "get_location_doc" is always undefined. When I replace that argument with a hard-coded ID, everything works just fine. I tried replacing "$subjectID" with simply subjectID (without quotes), $subjectID, "$$subjectID" and pretty much every variation you can think of.

What am I doing wrong?

Read more here:

Content Attribution

This content was originally published by yarden2100 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: