Alternative to $getField for MongoDB v4.4 aggregation

36 viewsaggregation frameworkmongodb
0

I’m using Atlas App Services which only supports MongoDB v4.4 and doesn’t include $getField as an aggregation operator. I need to access a field of an object returned by an expression, and feel this must be possible without $getField, but can’t figure out the syntax.

Simply, if I have an <expression>, what is the alternative to:

$getField: {
    field: "myFieldName",
    input: <expression>
}

The documentation implies $getField is only needed for unusual field names, beginning with a . or $.

A more complete example of the use case I need is below, where I want isOwner to be set to a boolean:

// Part of a $set in an aggregation
{ "isOwner" : {"$in" : [
    userId,
    {$getField: {
        field: "ownerIds",
        input: { $arrayElemAt: [ "$tasks", {
            $indexOfArray: [
                { $map: { input: "$tasks", as: "task", in: "$$task._id" }},
                "$$myId"
            ]
        }]}

    }}
]}}

There’s a bit going on here, but my main question is how can I achieve the same without $getField. I may also be over-complicating the inner part that extracts a particular task from within an array of tasks by matching an Id – so glad of any comments on that too!