$project: Is it possible to access a property of an expression result in just one single stage?

前端 未结 2 638
独厮守ぢ
独厮守ぢ 2021-01-20 06:21

Is there any way to carry out the following operation in just one $project stage?

db.getCollection(\'users\').aggregate([
    {
        $project         


        
2条回答
  •  清酒与你
    2021-01-20 06:57

    You need $let operator:

    db.getCollection('users').aggregate([
        {
            $project : {
                domain : {
                    $let: {
                        vars: { firstEmail: { $arrayElemAt : ["$emails", 0] } },
                        in: "$$firstEmail.domain"
                    }
                }
            }
        }
    ])
    

    Mongo Playground

提交回复
热议问题