在包含对另一个集合的查找(其中外部集合字段==给定值)后无法聚合字段的总和

问题描述 投票:0回答:1

我有 2 个 mongo 集合 - TransactionSupplier。我正在尝试对给定国家/地区内外的交易总价值进行汇总,以便我可以比较进口与国内购买。当我将供应商集合加入到交易中时,我的匹配条件似乎被忽略,并且返回了所有交易的总和。我做错了什么?

这是我的聚合,尝试计算所有交易的总和,其中 location.country == uk

let aggs = [
        {
            $match : { type : "out" },
        },
        {
            $lookup: {
                from:"suppliers",
                let: { country : "$location.country" },
                localField:"supplier",
                foreignField:"_id",
                as:"suppliers",
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $eq: [ '$suppliers.location.country', "United Kingdom" ]
                            }
                        },
                    },
                ]
            }
        },
        {
            $group: {
                _id: null,
                total: { $sum : { $divide: ['$price', 100] } }
            },
        }
    ];

这是我的交易模式


const TransactionSchema = mongoose.Schema({
    item: {type: String, required: true },
    category: {type: String },
    type: { type: String, enum: ['in', 'out'] },
    price: { type: Number, required: true, get: getPrice, set: setPrice },
    description: { type: String },
    supplier: {
        type: mongoose.SchemaTypes.ObjectId,
        ref: "Supplier"
    },
    customer: {
        type: mongoose.SchemaTypes.ObjectId,
        ref: "Customer"
    },
    project: { type: String },
    invoice_ref: { type: String },
    payment_method: { type: String },
    parent_transaction: {
        type: mongoose.SchemaTypes.ObjectId,
        ref: "Transaction"
    },
    date: { type: Date },
    createdAt: { type: Date, default: Date.now(), immutable: true },
    updatedAt: { type: Date, default: Date.now() }
});

这是我的供应商架构

const SupplierSchema = mongoose.Schema({
    name: {
        type: String, unique: true
    },
    description: { type: String },
    website: { type: String },
    email: { type: String },
    location: {
        address: { type: String },
        town: { type: String },
        county: { type: String },
        postcode: { type: String },
        country: { type: String }
    }
});
javascript mongodb mongoose
1个回答
0
投票

我找到了一种方法,不确定是否有更干净的方法

let aggs = [{
    $facet: {
        domestic: [
            {
                $match : { type : "out" },
            },
            {
                $lookup: {
                    from:"suppliers",
                    localField:"supplier",
                    foreignField:"_id",
                    as:"suppliers",
                }
            },
            { $unwind: "$suppliers" },
            { 
                $match: { 
                    $expr: { $eq: [ "$suppliers.location.country", "United Kingdom" ] } 
                }
            },
            {
                $group: {
                    _id: null,
                    total: { $sum : { $divide: ['$price', 100] } }
                }
            },
            {
                $project: { total: { $round: ["$total", 2]}  }
            }
        ],
        import: [
            {
                $match : { type : "out" },
            },
            {
                $lookup: {
                    from:"suppliers",
                    localField:"supplier",
                    foreignField:"_id",
                    as:"suppliers",
                }
            },
            { $unwind: "$suppliers" },
            { 
                $match: { 
                    $expr: { $ne: [ "$suppliers.location.country", "United Kingdom" ] } 
                }
            },
            {
                $group: {
                    _id: null,
                    total: { $sum : { $divide: ['$price', 100] } }
                }
            },
            {
                $project: { total: { $round: ["$total", 2]}  }
            }
        ]
    }
}];

那是我的回报

[{
    "domestic": [
        {
            "_id": null,
            "total": 12298.31
        }
    ],
    "import": [
        {
            "_id": null,
            "total": 8559.1
        }
    ]
}]

我注意到 $round 函数似乎四舍五入到小数点后两位,但它没有添加 0

© www.soinside.com 2019 - 2024. All rights reserved.