簡體   English   中英

如何在 MongoDB 中的對象數組中聚合項目

[英]How to aggregate an item in an array of objects in MongoDB

我不知道是否有人可以幫助我解決這個問題。 我正在嘗試使用 MongoDB 中的聚合方法在與 guarantorsId 匹配的對象數組中獲取單個項目。 我的意思是,有一個用戶集合:-:即

{
        "_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...

然后一個用戶申請貸款並提供另外兩個用戶作為擔保人。 “guarantorsId”是需要接受/拒絕成為擔保人的其他用戶的ID,而“_userId”是申請貸款的人。 即貸款催收:

{
        "_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",
    }

現在,我需要找到與 userId 匹配的所有貸款請求(即當 gAccepted 仍然為 false 時作為 guarantorOne 或 guarantorTwo,並且還需要從 users 集合中獲取 _userId(申請貸款的人(fisrtName 和 lastName))。我試過了,但沒有用;

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,
            }
        }

如果可能的話,我不確定我的收藏結構。 請任何有更好想法的人也歡迎

根據您的收藏,沒有 guarantorOne.gId 的密鑰,您應該使用guarantorOne.guarantorsId ,檢查下面的查詢

       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,
            }
        }])

下面的代碼也適用

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,
            }
        }]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM