如何在mongodb中将NumberDecimal值相乘

时间:2019-06-18 18:14:20

标签: mongodb mongoose many-to-many

我具有以下结构:

{
            "_id": "5d0118f0f57a282f89bc5f71",
            "product": {
                "_id": "5cfed37375a13067dd01ddb7",
                "name": "My product",
                "description": "My description",
                "purchased_amount": 15,
                "unit_price_mex": "45",
                "unit_price_to_sell": "5",
                "travel": "5cf58713d6f7f1657e2d8302",
                "__v": 0,
                "id": "5cfed37375a13067dd01ddb7"
            },
            "client": {
                "_id": "5cf1778efffb651fad89d8b6",
                "name": "Client name",
                "description": "",
                "__v": 0
            },
            "purchased_amount": 3,
            "fch": "13/6/2019",
            "__v": 0
        },
        {
            "_id": "5d0151afda1a446008f1817b",
            "product": {
                "_id": "5cfed1995eaf2665c45efd82",
                "name": "Camisa",
                "description": "Camisas buenas",
                "purchased_amount": 10,
                "unit_price_mex": "100",
                "unit_price_to_sell": "15",
                "travel": "5cf56b04462a865264fabb9d",
                "__v": 0,
                "id": "5cfed1995eaf2665c45efd82"
            },
            "client": {
                "_id": "5cf1778efffb651fad89d8b6",
                "name": "Randy",
                "description": "El que trabaja aqui",
                "__v": 0
            },
            "purchased_amount": 34,
            "fch": "12/6/2019",
            "__v": 0
        },

其中客户端和产品的类型为ObjectId。这是架构:

客户模型

var mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-paginate-v2');


var clientSchema = new mongoose.Schema({
        name: String,
        description: String
    }).plugin(mongoosePaginate);

var Client = mongoose.model('Client', clientSchema);

module.exports = Client;

产品型号


var mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-paginate-v2');

var productSchema = new mongoose.Schema({
        name: String,
        description: String,
        purchased_amount: Number,
        unit_price_mex: mongoose.Schema.Types.Decimal128,
        unit_price_to_sell: mongoose.Schema.Types.Decimal128, 
        travel: { type: mongoose.Schema.Types.ObjectId, ref: 'Travel' }
    }).plugin(mongoosePaginate);

productSchema.set('toJSON', {
        getters: true,
        transform: (doc, ret) => {
            if (ret.unit_price_mex) {
                ret.unit_price_mex = ret.unit_price_mex.toString();
            }
            if ( ret.unit_price_to_sell ) {
                ret.unit_price_to_sell = ret.unit_price_to_sell.toString();
            }
        }
    })

var Product = mongoose.model('Product', productSchema);

module.exports = Product;

我需要获得product_unit_price_to_sell的purchase_amount的乘积和。我的代码如下,但始终返回0。显然,“ $ product.unit_price_to_sell”不会返回十进制值。

    var aggregate = InvoiceModel.aggregate([
        { $match: { client: mongoose.Types.ObjectId( id ) } },
        { $group: { _id: null, total: { $sum: { $multiply: [ "$purchased_amount", "$product.unit_price_to_sell" ] } } } }
    ]);
    InvoiceModel.aggregatePaginate(aggregate, {}, (error, aggs) => {
        InvoiceModel.paginate({ client: id },{ page, limit, populate: 'client product' }, (err, value) => {
            return res.status(200).send({
                results: value.docs,
                totalPages: value.totalPages,
                totalDocs: value.totalDocs,
                purchase_amount_total : aggs.docs[0].total
            })
        })
    })

1 个答案:

答案 0 :(得分:0)

MongoDB无法在算术表达式中使用字符串值。您必须使用数值的非字符串表示形式存储值,或者必须使用$toDecimal之类的聚合运算符首先将值转换为其数值表示形式。

$group阶段修改为类似以下内容的方法应该起作用:

{ $group: { _id: null, total: { $sum: { $multiply: [ "$purchased_amount", { $toDecimal: "$product.unit_price_to_sell" } ] } } } 

但是请注意,这仅适用于> = 4.0的MongoDB版本。如果您使用的是MongoDB的旧版本,则需要将其升级到至少4.0版,或者开始将现有值从字符串转换为数字。