Can MongoDB replace referencing indices with actual Documents?

Hello, I am fairly new to MongoDB. Allow me to elaborate. I got something like this:

Collection Freelancer got 1 Document:

{
    _id: ...,
    index: 1,
    name: "Alfred",
    workingFor: [ 1, 2 ]   //these numbers in the array are referencing to "index" of Collection Company.
}

Collection Company got 2 Documents:

{
   _id: ...,
   index: 1,
   name: "Company A"
},
{
   _id: ...,
   index: 2,
   name: "Company B"
}

Upon querying, is it possible for MongoDB to automatically resolve for the index? For example, if I query for Alfred, instead of having data appearing like above, have it appear like this:

{
    _id: ...,
    index: 1,
    name: "Alfred",
    workingFor: [
        {
           index: 1,
           name: "Company A"
        },
        {
           index: 2,
           name: "Company B"
        },
    ]
}

On Node.js, with Mongoose, I am thinking I could find for Alfred on Collection Freelancer, then using the [ 1, 2 ] of workingFor, I could find for Company A and Company B, then remap the dictionary object to be what I want.

But before start implementing, does MongoDB have a built-in method for something like this already? This is so I don’t have to re-invent the wheel.

Thank you,
Iono.

Yes.

Look at https://docs.mongodb.com/manual/aggregation/

You might be interested to take the course MongoDB Courses and Trainings | MongoDB University

Hello @iono_sphere,

The MongoDB Aggregation Framework allows you to query two collections with referencing fields as a “join” operation. This is accomplished using the $lookup stage of an aggregation query.