0
votes

below is all the description collection 1 : users

"_id" : ObjectId("5e2977e1cc1208c65c00648b"),
        "mappedShops" : [
                ObjectId("5e2976cbcc1208c65c006488"),
                ObjectId("5e2976cbcc1208c65c00690c"),
                ObjectId("5e2976cbcc1208c65c006499")
       "phoneNo" : 6789012345,
        "name" : "acdbcs",
        "address" : "address 2",
        "shopCode" : "D3137",
        "state" : "M.P",
        "city" : "Indore"

NOTE: deatils of mappedShops for e.g: ObjectId("5e2976cbcc1208c65c00690c") is in the same users collection
collection 2 : orders

"_id" : ObjectId("5e27f998a42d441fe8a8957f"),
        "isApproved" : false,
"orderCreatedOn" : ISODate("2020-01-22T18:30:00Z"),
"shopOrder" : [],
"frequency" : "WE",
"orderCreatedBy" : ObjectId("5e2976cbcc1208c65c00690c")

Collection 3: payments

 "_id" :  ObjectId("5dd7900bcd00d33c245abbfa"),
 "paymentOfTheDay" : 400,
 "outstanding" : 100,
 "paymentDoneBy":ObjectId("5e2976cbcc1208c65c00690c")

scenario is : i will get _id(i.e admin id) from req.body Here is what i need to do 1: i need to know all the mappedShops objectId's & than for all those objectId's i need to find the details of all those objectId's 2: than i need to look what order that shop has created from order collection.(for all those id's) 3: than i need to look what is the outstanding from the payments collection(again for all the id's) Here is what i need to send to the frontend in the mentioned object Array {"name","phoneNo","address"}(from users collection)+{"orderCreatedOn": ISODate("2020-01-22T18:30:00Z"),"isApproved"}(from orders collection)+{"outstanding"}(from payments collection) expected response may look like this:

"shopsListDetails":[{
"phoneNo","name","address","shopCode" ,"isApproved","outstanding"
}]

POSTMAN REQUEST this is the id of admin

{
    "_id": "5e2977e1cc1208c65c00648b"
}

ultimately front end shall receive all the data which i mentioned in shopsListDetails[{}] for all the mappedShops in the user collection against that admin I am kind of very much stuck in this if any can help me out

2

2 Answers

0
votes

Considering the first schema you shared is of User

I am assuming You are storing order_id inside payments Schemama

You can write aggregate query like this

    User.aggregate([
        {
            $match:{
                _id:req.body.admin_id
            }
        },{
            $unwind:'$mappedShops'
        },{
            $lookup:{
                from:'users',
                localField:'mappedShops',
                foreignField:'_',
                as:'mappedShops'
            }
        },{
            $unwind:'$mappedShops'
        },{
            $lookup:{
                from:'orders',
                localField:'mappedShops._id',
                foreignField:'orderCreatedBy',
                as:'order'
            }
        },{
            $unwind:'$order'
        },{
            $lookup:{
                from:'payments',
                localField:'order._id',
                foreignField:'order_id',
                as:'payment'
            }
        },{
            $unwind:'$payment'
        },{
            $project:{
                _id:0,
                name:'$mappedShops.name',
                phoneNo:'$mappedShops.phoneNo',
                address:'$mappedShops.address',
                orderCreatedOn:'$order.orderCreatedOn',
                isApproved:'$order.isApproved',
                outstanding:'$payment.outstanding'
            }
        }
    ])
  1. Firstly, we unwind all the mappedShops & get individual objects of the same from Lookups | This gives us the User details for final data
  2. Then, we find matching order for each mappedshops from orders collection by $lookup & $unwind same | This gives us the order details for final data
  3. Lastly, We match order_id inside payments collection through $lookup & get payment status of each order | This gives us the payment status for final data

PS : For this to worker, you must have the entry of each order in both orders & payments collection, whether or not payment is completed

Comment below if you need more clarification / any change is needed here

0
votes

Please try this :

db.users.aggregate([
    {
        $match: {
            _id: req.body.adminID
        }
    }, {
        $lookup: {
            from: 'users',
            localField: 'mappedShops',
            foreignField: '_id',
            as: 'mappedShops'
        }
    }, { $unwind: '$mappedShops'}, { $replaceRoot: { newRoot: "$mappedShops" } },
    { 
       $lookup:
        {
            from: "orders",
            let: { mappedShopsId: "$_id" },
            pipeline: [
                {
                    $match: { $expr: { $eq: ["$orderCreatedBy", "$$mappedShopsId"] } }
                },
                { $project: { orderCreatedOn: 1, isApproved: 1 } }
            ],
            as: "orders"
        }
    },{
       $lookup:
        {
            from: "payments",
            let: { mappedShopsId: "$_id" },
            pipeline: [
                {
                    $match: { $expr: { $eq: ["$paymentDoneBy", "$$mappedShopsId"] } }
                },
                { $project: { outstanding: 1 } }
            ],
            as: "payments"
        }
    },
    { $project: { name: 1, phoneNo: 1, address: 1, shopCode: 1, orders: 1, payments: 1 } }
])

Test : MongoDB-Playground