从$arrayElemAt
内的$map
选择特定字段。
我只想选择$arrayElemAt
const data = await this.aggregate([
{
$match: { provider_id: providerId },
},
{
$lookup: {
from: 'users',
localField: 'staff.user_id',
foreignField: '_id',
as: 'staffUsers',
},
},
{
$project: {
staff: {
$map: {
input: '$staff',
in: {
_id: '$$this._id',
email_login: '$$this.email_login',
full_calendar_view: '$$this.full_calendar_view',
verified: '$$this.verified',
user_id: '$$this.user_id',
description: '$$this.description',
name: {
$arrayElemAt: [
'$staffUsers',
{
$indexOfArray: ['$staffUsers._id', '$$this.user_id'],
},
],
},
},
},
},
},
},
]);
答案 0 :(得分:0)
只需将.dot
与$staffUsers
一起使用
{ "name": {
"$arrayElemAt": [
"$staffUsers.name",
{ "$indexOfArray": ["$staffUsers._id", "$$this.user_id"] }
]
}}