简体   繁体   中英

How to aggregate an item in an array of objects in MongoDB

I don't know if someone could help me in solving this problem. I am trying to get a single item in an array of objects that matches a guarantorsId using the aggregation method in MongoDB. What I mean is, there is a users collection:-: ie

{
        "_id":1
        "loan":"0",
        "firstName":"kenneth",
        "lastName":"mac",
        "mobile":"09090000002",
        "email":"ken@gmail.com"
    }
    {
        "_id":2,
        "loan":"0",
        "firstName":"david",
        "lastName":"paul",
        "mobile":"09090000002",
        "email":"david@gmail.com"
    }
    ...more...

Then a user applies for a loan and provides two other users as guarantors. "guarantorsId" is the id of other users who needs to accept/decline to be a guarantor, while "_userId" is the person applying for the loan. ie loan collection:

{
        "_id":11aa,
        "guarantorOne":{
            "gAccepted":false,
            "guarantorsId":2
        },
        "guarantorTwo":{
            "gAccepted":false,
            "guarantorsId":9
        },
        "_userId":1,
        "amount":"5678",
        "duration":"12",
    }
    {
        "_id":"11bb",
        "guarantorOne":{
            "gAccepted":false,
            "guarantorsId":4
        },
        "guarantorTwo":{
            "gAccepted":false,
            "guarantorsId":2
        },
        "_userId":3,
        "amount":"5678",
        "duration":"12",
    }

Now, I need to find all loan request that matches a userId (ie either as guarantorOne or guarantorTwo when gAccepted is still false and also to get the _userId(the person who applied for loan(fisrtName and lastName)) from the users collection. I tried this but it didn't work;

await Loan.aggregate(
        { $match: {
                $or: [{"guarantorOne.gId": 2}, {"guarantorTwo.gId": 2}],
                $and: [{"guarantorOne.gAccepted": false}, {"guarantorTwo.gAccepted": false}]
            }   
        },{
            $lookup:
                {
                    from: "users",
                    localField: "_userId",
                    foreignField: "_id",
                    as: "user"
                }
        },{
            $unwind: "$user"
        },
        {   
            $project: {              
                "amount": 1,
                "duration": 1,   
                "user._id": 1,
                "user.firstName": 1,
                "user.lastName": 1,
                "user.middleName": 1,
            }
        }

I'm not to sure of my collection structure, if it is possible this way. Please anyone with better ideas is welcome too

As per your collection there is no key as guarantorOne.gId , you should use guarantorOne.guarantorsId , check below query

       await Loan.aggregate([{ $match: {
                $or: [{"guarantorOne.guarantorsId": 2}, {"guarantorTwo.guarantorsId": 2}],
                $and: [{"guarantorOne.gAccepted": false}, {"guarantorTwo.gAccepted": false}]
            }   
        },{
            $lookup:
                {
                    from: "users",
                    localField: "_userId",
                    foreignField: "_id",
                    as: "user"
                }
        },{
            $unwind: "$user"
        },
        {   
            $project: {              
                "amount": 1,
                "duration": 1,   
                "user._id": 1,
                "user.firstName": 1,
                "user.lastName": 1,
                "user.middleName": 1,
            }
        }])

This code below works as well

await Mainloan.aggregate([
        {
            $match: {
                $and: [
                    {
                        $or: [{"guarantorOne. guarantorsId": 2}, {"guarantorOne.gAccepted": false}],
                    },
                    {
                        $or: [{"guarantorTwo. guarantorsId": 2}, {"guarantorTwo.gAccepted": false}]
                    }
                ]
            }
        },
        { 
            $lookup:
                { 
                    from: "accounts",
                    localField: "_userId",
                    foreignField: "_id",
                    as: "user"
                }
        },
        {
            $unwind: "$user"
        },
        {   
            $project: {              
                "amount": 1,
                "duration": 1, 
                "user._id": 1,
                "user.firstName": 1,
                "user.lastName": 1,
                "user.middleName": 1,
            }
        }]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM