8

I want to get results like simple inner join from SQL, for get all accounts of the same id_customer. I almost do this, but I get something like left join, and I don't now how to filer the result of query.

query:

db.customer.aggregate([
    {
        $match: {
            "LOAN.AMOUNT": { $gte: 41000 }
        }
    },

    {
        $lookup: {
            from: "department",
            localField: "ID",
            foreignField: "ACCOUNT.ID_CUSTOMER",
            as: "customer"
        }
    },
    { $match: { "myArray": { $ne: [] } } },
    {
        $unwind: {
            path: "$customer",
            preserveNullAndEmptyArrays: false
        }
    }
    ,
    {
        $unwind: "$customer.ACCOUNT"
    },
    {
            $match: {
                "customer.ACCOUNT.ID_CUSTOMER": "$ID" 
            }
    },

]) 

actual result:

empty

expected result:

{ 
    "_id" : ObjectId("5dfccc28d29876c2988c7c05"), 
    "ID" : 4.0, 
    "SECOND_NAME" : "abc", 
    "FIRST_NAME" : "abc", 
    "ID_DISCOUNT" : {
        "ID" : 3.0, 
        "TITLE" : "abc", 
        "AMOUNT" : 5.0
    }, 
    "LOAN" : {
        "ID" : 4.0, 
        "AMOUNT" : 42750.0
    }, 
    "customer" : {
        "_id" : ObjectId("5dfb7f8f5861584bbaedf718"), 
        "ID" : 1.0, 
        "TITLE" : "abc", 
        "ADDRESS" : "abc", 
        "CITY" : "abc", 
        "ACCOUNT" : {
            "ID" : 10.0, 
            "ID_CUSTOMER" : 4.0, 
            "DATE_OPEN" : "2019-09-24 21:03:44"
        }
    }
}
{ 
    "_id" : ObjectId("5dfccc28d29876c2988c7c05"), 
    "ID" : 4.0, 
    "SECOND_NAME" : "abc", 
    "FIRST_NAME" : "abc ", 
    "ID_DISCOUNT" : {
        "ID" : 3.0, 
        "TITLE" : "abc", 
        "AMOUNT" : 5.0
    }, 
    "LOAN" : {
        "ID" : 4.0, 
        "AMOUNT" : 42750.0
    }, 
    "customer" : {
        "_id" : ObjectId("5dfb7f8f5861584bbaedf718"), 
        "ID" : 1.0, 
        "TITLE" : "abc", 
        "ADDRESS" : "abc", 
        "CITY" : "abc", 
        "ACCOUNT" : {
            "ID" : 11.0, 
            "ID_CUSTOMER" : 4.0, 
            "DATE_OPEN" : "2019-08-23 21:03:44"
        }
    }
}

List of all account with the same id_customer, but

$match: {
                    "customer.ACCOUNT.ID_CUSTOMER": "$ID" 
                }

not doing what I want to do.

1 Answer 1

7

I found the solution: use $expr operator

db.customer.aggregate([
    {
        $match: {
            "LOAN.AMOUNT": { $gte: 41000 }
        }
    },

    {
        $lookup: {
            from: "department",
            localField: "ID",
            foreignField: "ACCOUNT.ID_CUSTOMER",
            as: "customer"
        }
    },
    { $match: { "myArray": { $ne: [] } } },
    {
        $unwind: {
            path: "$customer",
            preserveNullAndEmptyArrays: false
        }
    }
    ,
    {
        $unwind: "$customer.ACCOUNT"
    },
    { $match: { $expr: { $eq: ["$customer.ACCOUNT.ID_CUSTOMER", "$ID"] } } },

])  
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.