0

I have collection with documents like this one:

{  
   "_id": 1,
   "people": [  
      {  
         "name": "Bob",
         "age": "15"
      },
      {  
         "name": "Alice",
         "age": "18"
      }
   ]
}

My query is:

db.groups.aggregate({ $match: { "_id": 1 }}, { $project: { "_id": 0, "people.name": 1 } })

This query returns:

{  
   "people": [  
      {  
         "name": "Bob"
      },
      {  
         "name": "Alice"
      }
   ]
}

But I need the result like:

{ "names": [ "Bob", "Alice" ] }

Which parameters should I add to the .aggregate() function?

2
  • 1
    No need to use $map here... Try this .groups.aggregate({ $match: { "_id": 1 }}, { $project: { "_id": 0, "names": "$people.name" } }) Commented Nov 23, 2018 at 15:16
  • Thanks! It's helped Commented Nov 23, 2018 at 15:22

1 Answer 1

1

The solution is:

db.groups.aggregate({ $match: { "_id": 1 }}, { $project: { "_id": 0, "names": "$people.name" } })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.