有没有办法写一个Mongo查询来返回日期&量?

时间:2017-02-08 16:39:44

标签: mongodb

我有几个"订单"在我的Mongo数据库中,用于各种客户端("组织")。每个订单可能包含1+付款给客户(例如2月1日 - 200美元,3月1日 - 200美元等)。

我试图弄清楚如何编写一个查看所有订单的查询,按组织划分并返回给我3个值:

输入: 组织(Mongo Field:destinationId)

输出:   - 收费日期(Mongo Field:dateCharge)   - 收费金额(Mongo Field:price)   - 免费存款(Mongo Fields(数学) - 价格减去totalFee)

如果在查询中进行数学计算太困难,那么我可以简单地使用totalFee字段并在Excel / Google表格中进行数学计算。

以下是我的示例文档:

{
    "_id" : ObjectId("588e79e2e7cd645918a27c82"),
    "userId" : "5840e880b41687d46829ee26",
    "orderId" : "1001GJ",
    "updateAt" : ISODate("2017-01-29T23:25:22.566Z"),
    "createAt" : ISODate("2017-01-29T23:25:22.566Z"),
    "paymentsPlan" : [ 
        {
            "_id" : ObjectId("588e79e2e7cd645918a27c7c"),
            "description" : "Payment 1 of 4",
            "feeProcessing" : 3.92,
            "feeCollections" : 5.77,
            "totalFee" : 9.69,
            "originalPrice" : 125,
            "basePrice" : 115.31,
            "price" : 125,
            "dateCharge" : ISODate("2017-01-29T23:25:22.092Z"),
            "destinationId" : "acct_17x0WQEVwZei3oeH",
            "version" : "v2",
            "updateAt" : ISODate("2017-01-29T23:30:04.393Z"),
            "createAt" : ISODate("2017-01-29T23:25:22.520Z"),
            "paymentMethods" : [ 
                "card", 
                "bank"
            ],
            "attempts" : [ 
                {
                    "message" : "done",
                    "status" : "succeeded",
                    "dateAttemp" : ISODate("2017-01-29T23:30:04.387Z"),
                    "_id" : ObjectId("588e7afce7cd645918a27c84")
                }
            ],
            "status" : "succeeded",
            "wasProcessed" : true,
            "discountCode" : "",
            "discount" : 0
        }, 
        {
            "_id" : ObjectId("588e79e2e7cd645918a27c7d"),
            "description" : "Payment 2 of 4",
            "feeProcessing" : 3.64,
            "feeCollections" : 5.3,
            "totalFee" : 8.94,
            "originalPrice" : 115,
            "basePrice" : 106.06,
            "price" : 115,
            "dateCharge" : ISODate("2017-01-29T23:25:22.092Z"),
            "destinationId" : "acct_17x0WQEVwZei3oeH",
            "version" : "v2",
            "updateAt" : ISODate("2017-01-29T23:35:04.316Z"),
            "createAt" : ISODate("2017-01-29T23:25:22.524Z"),
            "paymentMethods" : [ 
                "card", 
                "bank"
            ],
            "attempts" : [ 
                {
                    "message" : "done",
                    "status" : "succeeded",
                    "dateAttemp" : ISODate("2017-01-29T23:35:04.310Z"),
                    "_id" : ObjectId("588e7c28e7cd645918a27c86")
                }
            ],
            "status" : "succeeded",
            "wasProcessed" : true,
            "discountCode" : "",
            "discount" : 0
        }, 
        {
            "_id" : ObjectId("588e79e2e7cd645918a27c80"),
            "description" : "Payment 3 of 4",
            "feeProcessing" : 6.97,
            "feeCollections" : 10.62,
            "totalFee" : 17.59,
            "originalPrice" : 230,
            "basePrice" : 212.41,
            "price" : 230,
            "dateCharge" : ISODate("2017-02-10T16:00:00.000Z"),
            "destinationId" : "acct_17x0WQEVwZei3oeH",
            "version" : "v2",
            "updateAt" : ISODate("2017-02-08T15:57:22.891Z"),
            "createAt" : ISODate("2017-01-29T23:25:22.528Z"),
            "paymentMethods" : [ 
                "card", 
                "bank"
            ],
            "attempts" : [],
            "status" : "pending",
            "wasProcessed" : false,
            "discountCode" : "",
            "discount" : 0
        }, 
        {
            "_id" : ObjectId("588e79e2e7cd645918a27c81"),
            "description" : "Payment 4 of 4",
            "feeProcessing" : 6.97,
            "feeCollections" : 10.62,
            "totalFee" : 17.59,
            "originalPrice" : 230,
            "basePrice" : 212.41,
            "price" : 230,
            "dateCharge" : ISODate("2017-02-24T16:00:00.000Z"),
            "destinationId" : "acct_17x0WQEVwZei3oeH",
            "version" : "v2",
            "updateAt" : ISODate("2017-02-08T15:57:26.081Z"),
            "createAt" : ISODate("2017-01-29T23:25:22.529Z"),
            "paymentMethods" : [ 
                "card", 
                "bank"
            ],
            "attempts" : [],
            "status" : "pending",
            "wasProcessed" : false,
            "discountCode" : "",
            "discount" : 0
        }
    ],
    "status" : "active",
    "description" : "Boys 18U",
    "__v" : 2
}

我知道这是一个新手问题,但我自己资助我的业务,​​我的开发人员已经出去一段时间了,客户问我这个信息。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

我和我们的开发人员合作解决了这个问题。不确定它是否效率最高但是有效。

var mapFunction1 = function () {
    for (var idx = 0; idx < this.paymentsPlan.length; idx++) {
        if (this.paymentsPlan[idx].status === 'pending') {
            var key = this.paymentsPlan[idx].dateCharge.toISOString().substring(0, 10);
            var price = this.paymentsPlan[idx].price - this.paymentsPlan[idx].totalFee;
            emit(key, price);
        }

    }

};

var reduceFunction1 = function (dateCharge, price) {
    return Array.sum(price);
};

db.orders.mapReduce(
    mapFunction1,
    reduceFunction1,
    {
        out: { inline: 1 },
        query: { "paymentsPlan.destinationId": "acct_17vBpJHXmwMXUx1q"
        }
    }
)