Node.js 如何在MongoDB中合并两个集合:事务和税收

Node.js 如何在MongoDB中合并两个集合:事务和税收,node.js,mongodb,aggregation-framework,Node.js,Mongodb,Aggregation Framework,我有两个收款和交易税 示例:这些模型包括: Transaction { "_id" : ObjectId("5cff102b4ef90140801af1e9"), "totalPrice" : 3.32, "number" : 17, "__v" : 0, "taxes" : [ { "_id" : ObjectId("5cff104b4ef90140801af211"), "tax" :

我有两个收款和交易税

示例:这些模型包括:

Transaction
{
    "_id" : ObjectId("5cff102b4ef90140801af1e9"),
    "totalPrice" : 3.32,
    "number" : 17,
    "__v" : 0,
    "taxes" : [ 
        {
            "_id" : ObjectId("5cff104b4ef90140801af211"),
            "tax" : ObjectId("5b60ba0b6e7a8a3a101ea73a"),
            "taxAmount" : 0.21,
            "taxBase" : 1,
            "percentage" : 21
        }, 
        {
            "_id" : ObjectId("5cff104b4ef90140801af210"),
            "tax" : ObjectId("5bb1932f2db234342831f99e"),
            "taxAmount" : 0.11,
            "taxBase" : 1,
            "percentage" : 10.5
        }
    ]
}

Taxes
{
    "_id" : ObjectId("5b60ba0b6e7a8a3a101ea73a"),
    "name" : "IVA",
    "percentage" : 21
},
{
    "_id" : ObjectId("5bb1932f2db234342831f99e"),
    "name" : "IVA 10.5",
    "percentage" : 10.5
}
我想咨询一下,结果,把我还给你

{
    "_id" : ObjectId("5cff102b4ef90140801af1e9"),
    "totalPrice" : 3.32,
    "number" : 17,
    "__v" : 0,
    "taxes" : [ 
        {
            "_id" : ObjectId("5cff104b4ef90140801af211"),
            "tax" : {
                "_id" : ObjectId("5b60ba0b6e7a8a3a101ea73a"),
                "name" : "IVA",
                "percentage" : 21
            },
            "taxAmount" : 0.21,
            "taxBase" : 1,
            "percentage" : 21
        }, 
        {
            "_id" : ObjectId("5cff104b4ef90140801af210"),
            "tax" : {
                "_id" : ObjectId("5bb1932f2db234342831f99e"),
                "name" : "IVA 10.5",
                "percentage" : 10.5
            }
            "taxAmount" : 0.11,
            "taxBase" : 1,
            "percentage" : 10.5
        }
    ]
}
我的问题到此为止

db.getCollection('transactions').aggregate(
[{
        "$match": {
            "_id": ObjectId("5cff102b4ef90140801af1e9")
        }
    },
    {
        "$lookup": {
            "from": "taxes",
            "let": {
                "pid": "$taxes.tax"
            },
            "pipeline": [{
                "$match": {
                    "$expr": {
                        "$in": ["$_id", "$$pid"]
                    }
                }
            }],
            "as": "taxes"
        }
    }
])
但我的结果是

{
    "_id" : ObjectId("5cff102b4ef90140801af1e9"),
    "totalPrice" : 3.32,
    "number" : 17,
    "__v" : 0,
    "taxes" : [ 
        {
            "_id" : ObjectId("5bb1932f2db234342831f99e"),
            "name" : "IVA 10.5",
            "percentage" : 10.5
        }, 
        {
            "_id" : ObjectId("5bb1932f2db234342831f99e"),
            "name" : "IVA 10.5",
            "percentage" : 10.5
        }
    ]
}

它没有给我带来财政模型之外的字段,“税额”、“税基”和“百分比”。为什么会发生这种情况,而我没有得到agregate中关系的关系

您需要使用合并两个数组。要组合这两个对象,您可以使用,因为
$filter
返回一个数组,您可以使用该数组获取第一个项目。尝试:

db.transaction.aggregate([
    {
        "$match": {
            "_id": ObjectId("5cff102b4ef90140801af1e9")
        }
    },
    {
        $lookup: {
            from: "taxes",
            localField: "taxes.tax",
            foreignField: "_id",
            as: "taxDetails"
        }
    },
    {
        $addFields: {
            taxes: {
                $map: {
                    input: "$taxes",
                    as: "t",
                    in: {
                        $mergeObjects: [
                            "$$t",
                            {
                                tax: {
                                    $arrayElemAt: [
                                        {
                                            $filter: {
                                                input: "$taxDetails",
                                                as: "td",
                                                cond: {
                                                    $eq: [ "$$td._id", "$$t.tax" ]
                                                }
                                            }
                                        }, 0
                                    ]
                                }
                            }
                        ]
                    }
                }
            }
        }
    },
    {
        $project: {
            "taxDetails": 0
        }
    }
])