I have 4 collections, I want data from this collection for my Cart Page
- User
- Items
- ZipCode
- Provider
Here is the model of my collections
UserCollection : { _id :1, cart:[{ itemsId:10 }], zipcode:{ userZipCodeId: 0000 } }
ItemsCollection: [{ _id: 10, title: itemstitle }]
ZipCodeCollection:[{ _id: 0000, placeName: NY,serviceProviders:[100,101,102] }]
Providers :[ { _id: 100, title: Happy Store},{ _id: 101, title: ABC Store}]
On my cart page, I want user cart Items data, check user Zipcode and search from ZipCodeCollection which are the service providers in the zip code, then get the provider info from the Provider collection
here is my code
User.aggregate([
{ $match: { _id: req.user._id } },
{
$lookup:
{
from: 'ItemsCollection',
localField: 'cart',
foreignField: '_id',
as : "cartList"
},
},
{
$lookup:
{
from: 'zipcode',
localField: 'zipcode.userZipCodeId',
foreignField: '_id',
as : "zipcodeList"
},
},
{
$lookup:
{
from: 'providers',
localField: 'zipcodeList.providers',
foreignField: '_id',
as : "providersList"
},
},
{"$match":{"providersList.status": true }},
{
$project:
{
_id:1,
pincode:1,
email:1,
name:1,
cartList:1,
zipcodeList:1,
cartList:1
}
}
here it’s working, but take more time ( more than seconds )to execute I am afraid about the performance and bad design, how to increase this code performance and design well