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

前端 未结 2 640
独厮守ぢ
独厮守ぢ 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 07:07

    When working with array of objects, you can automatically get array of objects's properties with the dot notation. So the following will perfectly work :

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

    Mongo playground

提交回复
热议问题