Du skal først $unwind
employee
så kan du bruge $concat
at kombinere to felter.
db.loggableUser.aggregate([
{ '$lookup': {
'from': 'employees',
'localField': 'employee',
'foreignField' : '_id',
'as': 'employee'
}},
{ '$unwind': '$employee' },
{ '$addFields': {
'employee.fullName' : { '$concat': ['$employee.name' + ' ' + '$employee.surname'] }
}}
])
Hvilket er langt nemmere med mongodb 3.6 og derover $lookup
syntaks.
db.loggableUser.aggregate([
{ '$lookup': {
'from': 'employees',
'let': { 'employee': '$employee' },
'pipeline': [
{ '$match': { '$expr': { '$eq': ['$_id', '$$employee'] }}},
{ '$addFields': { 'fullName' : { '$concat': ['$name', ' ', '$surname'] }}}
]
'as': 'employee'
}}
])