MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Proietta il primo elemento di una matrice in un nuovo campo (aggregazione MongoDB)

Aggiornamento:

A partire dalla v4.4 esiste un operatore dedicato $first:

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

È uno zucchero di sintassi per il

Risposta originale:

Puoi usare arrayElemAt:

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