Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
How to return property of all elements of array inside MongoDB document?

I want to return the title and _id properties of each element in my trips array of a user's document. I am attempting to use the aggregate pipeline but its my first time and the docs are overwhelming. It seems like all the examples show how to find a single piece of data. I basically want to return something like


[
  {title: "one", _id: 123},
  {title: "two", _id: 456},
  {title: "three", _id: 789},
]

from my document that looks like:

enter image description here

Thank you in advance for any suggestions.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

function ObjArray(array){
    let newArr = [];
    array.foreach(
    (obj) => {
        let newobj = {title: '', id:''}
        newobj.title = obj.title;
        newobj.id = obj.id;
        newArr.append(newobj);
    }
    );
    return newArr;
}
about 4 years ago · Santiago Gelvez Report

0

You can use find({}) for getting your expected result. In find query, you need to use a projection stage.

Here the demo

Expected query

db.collection.find({},{_id:1,"trips.title":1,"trips._id":1});
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!