90

I am using Mongoose aggregation (MongoDB version 3.2).

I have a field users which is an array. I want to $project first item in this array to a new field user.

I tried

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

  { $project: {
    user: '$users.0',
    otherField: 1
  }},

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

But neither works.

How can I do it correctly? Thanks

Hongbo Miao
  • 45,290
  • 60
  • 174
  • 267
  • Possible duplicate of [Get n-th element of an array in MongoDB](http://stackoverflow.com/questions/7223273/get-n-th-element-of-an-array-in-mongodb) – styvane Aug 28 '16 at 23:21

3 Answers3

179

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
}},
Alex Blex
  • 34,704
  • 7
  • 48
  • 75
19

If it is an array of objects and you want to use just single object field, ie:

{
 "users": [
    {name: "John", surname: "Smith"},
    {name: "Elon", surname: "Gates"}
   ]
}

you can use:

{ $project: { user: { $first: "$users.name" } } 

Edit (exclude case - after comment from @haytham)

In order to exclude a single field from a nested document in array you have to do 2 projections:

 { $project: { user: { $first: "$users" } } 

Which return whole first object, and then exclude field you do not want, ie:

{ $project: { "user.name" : 0 } 
Michał Szkudlarek
  • 1,443
  • 1
  • 21
  • 35
  • how can we exclude the match? I mean how to pass the 0 after that? { $project: { user: { $arrayElemAt: [ "$users", 0 ] }, otherField: 1 }}, or { $project: { user: { $first: "$users.name" } } – haytham Jul 14 '21 at 20:35
  • @haytham - added example to the answer. I think this cannot be done in a single projection ($first + exclude field from the result of first). – Michał Szkudlarek Jul 15 '21 at 06:58
11

Starting Mongo 4.4, the aggregation operator $first can be used to access the first element of an array:

// { "users": ["Jean", "Paul", "Jack"] }
// { "users": ["Claude"] }
db.collection.aggregate([
  { $project: { user: { $first: "$users" } } }
])
// { "user" : "Jean" }
// { "user" : "Claude" }
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190