Aggregation of Object with reference from another collection in Object

I have 2 main collections, first one is Customers, that contain objects in this type:

    {
    firstName:String,
    lastName:String,
    phone:String,
    ....
    }

And other collection with Properties, in this type:

    {
    name:String,
    address:String,
    notes:String,
    zipCode:Int32,
    ...
    owner:{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'customers'
     }
    }

When getting all properties, I want to know the first&last name of each one, but not sure how can I do it in once ? or should I index it with cron job ? how do I do that ?

Hello @David_Tayar, welcome back to the MongoDB Community forum!

As you are using Mongoose, you can use the populate(), which says:

MongoDB has the join-like $lookup aggregation operator in versions >= 3.2. Mongoose has a more powerful alternative called populate(), which lets you reference documents in other collections.

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s).

As an alternative, you can also use Mongoose Aggregate lookup.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.