0

大家好,我有3张表,

month:{
        number: Number,
        startTime: Date,
        endTime: Date,
        days: [{ type: mdb.Schema.Types.ObjectId, ref: ‘Day’ }],
    }
days:

{      number: Number,
        startTime: Date,
        endTime: Date,
        orders: [{ type: mdb.Schema.Types.ObjectId, ref: ‘Order’ }],
    }
orders:{
     number:Number,

      createdAt: { type: Date, default: Date.now },
detail:[],
total:Number,
totaTax:Number,
totalWithTax:Number,

}

我做三张表关联查询的时候,速度非常慢。请教一下大家有没有什么办法可以优化。
查询语句:
months.aggregate([{
‘$match’: {
_id: {‘$in’: [ObjectId(“604f53bf3072cf1ad1cd058c”),ObjectId(“60656886f466c3cfb2595932”)]}
}
}, {
‘$project’: {
_id: 0,
days: 1
}
}, {
‘$lookup’: {
from: ‘days’,
let: {
dayId: ‘$days’
},
pipeline: [{
‘$match’: {
‘$expr’: {
‘$in’: [‘$_id’, ‘$$dayId’]
}
}
}, {
‘$lookup’: {
from: ‘orders’,
let: {
orderId: ‘$orders’
},
pipeline: [{
‘$match’: {
‘$expr’: {
‘$in’: [‘$_id’, ‘$$orderId’]
}
}
}, {
‘$project’: {
_id: 0,
total: 1,
totaTax: 1,
totalWithTax: 1
}
}
],
as: ‘orders’
}
}, {
‘$project’: {
_id: 0,
orders: 1
}
}
],
as: ‘days’
}
}, {
‘$unwind’: {
path: ‘$days’
}
}, {
‘$project’: {
orders: ‘$days.orders’
}
}, {
‘$unwind’: {
path: ‘$orders’
}
}, {
‘$group’: {
_id: null,
total: {
‘$sum’: ‘$orders.total’
},
totalTax: {
‘$sum’: ‘$orders.totalTax’
},
totalWithTax: {
‘$sum’: ‘$orders.totalWithTax’
}
}
}
])

各位大侠有什么优化建议吗?
非常感谢

已回答的问题