Project first item in an array to new field (MongoDB aggregation)

Update:

Starting from v4.4 there is a dedicated operator $first:

{ $project: {
    user: { $first: "$users" },
    otherField: 1
}},

It’s a syntax sugar to the

Original answer:

You can use arrayElemAt:

{ $project: {
    user: { $arrayElemAt: [ "$users", 0 ] },
    otherField: 1
}},

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)