Forgive me if I confuse some terminology here, but I'm performing a join operation in an aggregation using the '$lookup' operator as shown here:
db.collection('items').aggregate([{$match: {}},
{
    $lookup: {
        from: 'usr',
        localField: 'usr._id',
        foreignField: '_id',
        as: '__usr'
    }
}, {
    $project: {
        info: 1,
        timestamp: 1,
        usr: {
            "$arrayElemAt": [ "$__usr", 0 ]
        }
    }
}], (err, result) => {
    res.json(result);
    db.close();
});
I'm performing a projection on the aggregation result, and I'm using '$arrayElemAt' to extract the single 'usr' match from the resulting array. For obvious reasons, I don't want to return the entire 'usr' record which contains sensitive information. What I'm looking to do is perform a projection on the element returned using the '$arrayElemAt' operation. The only way I've been able to accomplish this is to use an additional projection of the original projection, like so:
db.collection('items').aggregate([{$match: {}},
{
    $lookup: {
        from: 'usr',
        localField: 'usr._id',
        foreignField: '_id',
        as: '__usr'
    }
}, {
    $project: {
        info: 1,
        timestamp: 1,
        usr: {
            "$arrayElemAt": [ "$__usr", 0 ]
        }
    }
}, {
    $project: {
        info: 1,
        timestamp: 1,
        usr: {
            "username": 1
        }
    }
}], (err, result) => {
    res.json(result);
    db.close();
});
Is there a way to accomplish this without the duplicated projection?
You can assign the $arrayElemAt returned value to a variable using the $let expression and use dot notation to access the subdocument field in the in expression.
"usr": {
    "$let": {
        "vars": { 
            "field": { 
                "$arrayElemAt": [ "$__usr", 0 ]
            }
        },
        "in": "$$field.username"
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With