U moet eerst $unwind
de employee
dan kun je $concat
gebruiken
om twee velden te combineren.
db.loggableUser.aggregate([
{ '$lookup': {
'from': 'employees',
'localField': 'employee',
'foreignField' : '_id',
'as': 'employee'
}},
{ '$unwind': '$employee' },
{ '$addFields': {
'employee.fullName' : { '$concat': ['$employee.name' + ' ' + '$employee.surname'] }
}}
])
Wat veel gemakkelijker is met de mongodb 3.6 en hoger $lookup
syntaxis.
db.loggableUser.aggregate([
{ '$lookup': {
'from': 'employees',
'let': { 'employee': '$employee' },
'pipeline': [
{ '$match': { '$expr': { '$eq': ['$_id', '$$employee'] }}},
{ '$addFields': { 'fullName' : { '$concat': ['$name', ' ', '$surname'] }}}
]
'as': 'employee'
}}
])