我有2个集合交易和税收。

示例模型为:

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


它不会带给我财政模型之外的字段“ taxAmount”,“ taxBase”和“ percentage”。为什么会发生这种情况,而我却无法获得总体中的关系

最佳答案

您需要使用$map$filter合并两个数组。要合并两个对象,可以使用$mergeObjects,并且由于$filter返回一个数组,因此可以使用$arrayElemAt来获取第一项。尝试:

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


Mongo Playground

09-25 16:15