Mongoose populate nested array with aggregate

Viewed 27

I'm learning mongoose and I'm trying to populate everything from the account schema with the aggregate function.

I'm getting some trouble when I'm trying to populate the fields $account.sessions.trades.closes.timeframe and $account.sessions.trades.tag. The goal of the query is to get something like this

[
{ // trade 1
  name : "account-name",
  session : {name : "session-name"},
  trade : {
     symbol : {...},
     open : {....},
     closes : {
       gain : "12",
       timeframe : {time : 12, unit : "min"}
     },
     tags  : [
       {name: "tag-name-1"},
       {name: "tag-name-2"}
     ],

  }
},
{ // trade 2
 ...
},
{ // trade 3 
...
}
]

Account

const accountSchema = new Schema({
    name     : { type : String, required: true},
    sessions : [{
        type : Schema.Types.ObjectId,
        ref  : "Session_Model"
    }],
});

Session

const sessionSchema = new Schema({
    name         : {type : String}, 
    trades       : [{
        type : Schema.Types.ObjectId,
        ref  : "Trade_Model"
    }],
});

Trade

var operationSchema = new Schema({
    gain        : { type: Number },
    timeframe   : { type: Schema.Types.ObjectId, ref : "Timeframe_Model"},
});

const tradeSchema = new Schema({
    symbol          : { type: Schema.Types.ObjectId, ref : "Symbol_Model"},
    open            : operationSchema,
    closes          : [operationSchema],
    tags            : [{type: Schema.Types.ObjectId, ref : "Tag_Model"}],
});

Timeframe

const timeframeSchema = new Schema({
    time       : { type: Number, required: true },
    unit       : { type: String, required: true },
});

Aggregate query

Account_Model.aggregate(
                [
                    {$lookup : {
                        from         : "session_models",
                        localField   : "sessions",
                        foreignField : "_id",
                        as           : "populate_sessions"
                    }},
                    {$lookup : {
                        from         : "trade_models",
                        localField   : "populate_sessions.trades",
                        foreignField : "_id",
                        as           : "trade"
                    }},
                    
                    
                    {$unwind  : "$trade"},
                    {$sort    : {"trade.open.date" : 1}},

                    {$skip    : Number(req.params.limit) * (Number(req.params.page)-1)},   
                    {$limit   : Number(req.params.limit)},   

                    {$lookup : {
                        from         : "symbol_models",
                        localField   : "trade.symbol",
                        foreignField : "_id",
                        as           : "trade.symbol"
                    }},      
                    {$unwind : { path : "$trade.symbol"}},
                    {$lookup : {
                        from         : "timeframe_models",
                        localField   : "trade.open.timeframe",
                        foreignField : "_id",
                        as           : "trade.open.timeframe"
                    }},
                    {$unwind : { path : "$trade.open.timeframe"}},
                    {$lookup : {
                        from         : "session_models",
                        localField   : "trade.session_id",
                        foreignField : "_id",
                        as           : "session"
                    }},
                    {$unwind : { path : "$session"}},
                    {$match   : {}},
                    {$project : {
                        "name"          : 1,           
                        "trade"         : 1, 
                        "session.name"  : 1,                        
                        }
                    }
                ]
            ).then(accounts => { ... })

Thanks for your help.

0 Answers
Related