Mongodb本机查询和等效的Java代码

时间:2016-08-11 06:00:48

标签: mongodb

JSON DO
{ 
    "_id" : "t6Y596WHx44S", 
    "pos_txn_type" : "CREDIT_AUTHORIZATION", 
    "pos_payment_method" : "CREDIT_CARD", 
    "processing_status" : "APPROVED", 
    "route" : NumberInt(7), 
    "audit_info" : {
        "version" : "1.0.0.0", 
        "created_on" : ISODate("2016-08-08T07:26:57.000+0000"), 
        "updated_on" : ISODate("2016-08-08T07:26:57.000+0000")
    }, 
    "inflight_transactions" : [
        {
            "message_exchange" : {
                "service_type" : "GATEWAY_SERVICE", 
                "adapter_id" : "adpIsoUms", 
                "route1" : NumberInt(7), 
                "request" : {
                    "type" : "com.renovite.ripps.kernel.msg.GatewayRequest", 
                    "acquirer_inst_id" : "00000000001", 
                    "host_address" : "127.0.0.1:2000", 
                    "client_address" : "127.0.0.1:58577", 
                    "domain_request" : {
                        "type" : "com.renovite.ripps.kernel.msg.CardRequestMessage", 
                        "card" : {
                            "masked_pan" : "411111xxxxx1111", 
                            "card_type" : "VISA"
                        }, 
                        "transactionAmount" : {
                            "amount" : NumberInt(100), 
                            "amount_type" : "TXN_AMOUNT", 
                            "currency" : "USD", 
                            "currency_iso" : "840", 
                            "currency_minor_unit" : NumberInt(2)
                        }, 
                        "additionalAmountMap" : {

                        }, 
                        "pos" : {
                            "stan" : "470641215751", 
                            "entry_mode" : "SWIPED", 
                            "pos_environment" : "ATTENDED", 
                            "card_holder_verification_method" : "UNKNOWN", 
                            "pos_card_holder_verification_method" : "MANUAL_SIGNATURE", 
                            "terminal_capability" : "MSR_MANUAL", 
                            "pos_terminal_id" : "0000000000000001", 
                            "pos_transaction_time" : ISODate("2016-08-08T07:26:55.000+0000"), 
                            "pos_transaction_date" : ISODate("2016-08-08T07:26:55.000+0000"), 
                            "pos_function_code" : "100", 
                            "cp" : true
                        }, 
                        "billTo" : {
                            "postal_code" : "94538"
                        }
                    }, 
                    "merchant_details" : {
                        "merchant_code" : "00000000001", 
                        "merchant_category_code" : "5965", 
                        "merchant_postal_code" : "4567", 
                        "device_code" : "0000000000000001"
                    }, 
                    "auditInfo" : {
                        "version" : "1.0.0.0", 
                        "created_on" : ISODate("2016-08-08T07:26:57.000+0000")
                    }, 
                    "additionalAttributes" : {

                    }, 
                    "raw_header" : "ISO.UMS.03", 
                    "retrival_reference_number" : "000001215751"
                }, 
                "response" : {
                    "type" : "com.renovite.ripps.kernel.msg.GatewayResponse", 
                    "auditInfo" : {
                        "version" : "1.0.0.0", 
                        "created_on" : ISODate("2016-08-08T07:27:01.000+0000")
                    }, 
                    "additionalAttributes" : {

                    }, 
                    "domain_response" : {
                        "type" : "com.renovite.ripps.kernel.msg.CardResponseMessage", 
                        "auth_code" : "831000", 
                        "amount" : 9601.0, 
                        "currency" : "USD", 
                        "transaction_time" : ISODate("2016-08-08T01:57:00.000+0000"), 
                        "processor_ref_number" : "4706412203976900504007", 
                        "approval_code" : "0000", 
                        "avs_response_code" : "2", 
                        "reconciliation_id" : "4706412203976900504007", 
                        "partial_auth" : false
                    }
                }
            }
        }, 
        {
            "message_exchange" : {
                "service_type" : "FRAUD_SERVICE", 
                "adapter_id" : "cartFRAUD", 
                "request" : {
                    "type" : "com.renovite.ripps.kernel.msg.ProviderRequest", 
                    "auditInfo" : {
                        "version" : "1.0.0.0", 
                        "created_on" : ISODate("2016-08-08T07:26:58.000+0000")
                    }, 
                    "additionalAttributes" : {
                        "MerchantTransactionIdentifier" : "t6Y596WHx44S"
                    }, 
                    "retrivalReferenceNumber" : "000001215751", 
                    "merchantDetails" : {
                        "merchant_code" : "00000000001", 
                        "merchant_category_code" : "5965", 
                        "partial_auth" : "Y"
                    }, 
                    "domain_request" : {
                        "type" : "com.renovite.ripps.kernel.msg.fraud.FraudRequestMessage", 
                        "client_address" : "127.0.0.1:58577"
                    }
                }, 
                "route" : "6", 
                "processing_status" : "APPROVED", 
                "response" : {
                    "type" : "com.renovite.ripps.kernel.msg.ProviderResponse", 
                    "domain_response" : {
                        "type" : "com.renovite.ripps.kernel.msg.fraud.FraudResponseMessage", 
                        "fraud_score" : NumberInt(22)
                    }, 
                    "auditInfo" : {
                        "version" : "1.0.0.0", 
                        "created_on" : ISODate("2016-08-08T07:26:58.000+0000")
                    }, 
                    "additionalAttributes" : {

                    }
                }
            }
        }, 
        {
            "message_exchange" : {
                "service_type" : "AUTH_SERVICE", 
                "adapter_id" : "cartVACP", 
                "request" : {
                    "type" : "com.renovite.ripps.kernel.msg.ProviderRequest", 
                    "auditInfo" : {
                        "version" : "1.0.0.0", 
                        "created_on" : ISODate("2016-08-08T07:26:58.000+0000")
                    }, 
                    "additionalAttributes" : {
                        "COMMERCEINDICATOR" : "retail", 
                        "MerchantTransactionIdentifier" : "t6Y596WHx44S", 
                        "ThirdPartyCertificationNumber" : "575357012698"
                    }, 
                    "retrivalReferenceNumber" : "000001215751", 
                    "merchantDetails" : {
                        "merchant_code" : "00000000004", 
                        "merchant_category_code" : "5965", 
                        "partial_auth" : "Y"
                    }, 
                    "domain_request" : {
                        "type" : "com.renovite.ripps.kernel.msg.CardRequestMessage", 
                        "card" : {
                            "masked_pan" : "411111xxxxx1111", 
                            "card_type" : "VISA"
                        }, 
                        "transactionAmount" : {
                            "amount" : 9601.0, 
                            "amount_type" : "TXN_AMOUNT", 
                            "currency" : "USD", 
                            "currency_iso" : "840", 
                            "currency_minor_unit" : NumberInt(2)
                        }, 
                        "additionalAmountMap" : {
                    }, 
                    "pos" : {
                        "entry_mode" : "SWIPED", 
                        "pos_terminal_id" : "0000000087654321", 
                        "cp" : true
                    }, 
                    "billTo" : {
                        "postal_code" : "94538"
                    }
                }, 
                "merchant_code" : "v5p234p575357"
            }, 
            "route" : "3", 
            "processing_status" : "APPROVED", 
            "response" : {
                "type" : "com.renovite.ripps.kernel.msg.ProviderResponse", 
                "status" : "ACCEPT", 
                "domain_response" : {
                    "type" : "com.renovite.ripps.kernel.msg.CardResponseMessage", 
                    "auth_code" : "831000", 
                    "amount" : 9601.0, 
                    "currency" : "USD", 
                    "transaction_time" : ISODate("2016-08-08T01:57:00.000+0000"), 
                    "processor_ref_number" : "4706412203976900504007", 
                    "approval_code" : "100", 
                    "avs_response_code" : "2", 
                    "reconciliation_id" : "4706412203976900504007", 
                    "partial_auth" : false
                }, 
                "auditInfo" : {
                    "version" : "1.0.0.0", 
                    "created_on" : ISODate("2016-08-08T07:27:01.000+0000")
                }, 
                "additionalAttributes" : {
                    "PYMT_NETWORK_TXN_ID" : "016153570198200", 
                    "RECEIPT_NUMBER" : "138115", 
                    "REQUEST_TOKEN" : "Ahj/7wSR/kUuB8C84NOOelmjdg2aMWTJgzct2zlgwasGjBg3S36RRSYgFLfpFFJi0gp1MyMJsMmkmW6QHhMgQJyP8ilwPgXnBpxwvBkj", 
                    "REASONCODE" : NumberInt(100), 
                    "PROCESSORRESPONSE" : "00", 
                    "CARDCATEGORY" : "A", 
                    "MERCHANTREFERENCECODE" : "000001215751", 
                    "CARDGROUP" : "0"
                }
            }
        }
    }, 
    {
        "message_exchange" : {
            "service_type" : "LOYALTY_SERVICE", 
            "adapter_id" : "cartLOYALTY", 
            "request" : {
                "type" : "com.renovite.ripps.kernel.msg.ProviderRequest", 
                "auditInfo" : {
                    "version" : "1.0.0.0", 
                    "created_on" : ISODate("2016-08-08T07:27:01.000+0000")
                }, 
                "additionalAttributes" : {
                    "MerchantTransactionIdentifier" : "t6Y596WHx44S"
                }, 
                "retrivalReferenceNumber" : "000001215751", 
                "merchantDetails" : {
                    "merchant_code" : "00000000001", 
                    "merchant_category_code" : "5965", 
                    "partial_auth" : "Y"
                }, 
                "domain_request" : {
                    "type" : "com.renovite.ripps.kernel.msg.loyalty.LoyaltyRequestMessage", 
                    "amount" : 9601.0, 
                    "client_address" : "127.0.0.1:58577"
                }
            }, 
            "route" : "7", 
            "processing_status" : "APPROVED", 
            "response" : {
                "type" : "com.renovite.ripps.kernel.msg.ProviderResponse", 
                "domain_response" : {
                    "type" : "com.renovite.ripps.kernel.msg.loyalty.LoyaltyResponseMessage", 
                    "loyaltyPoint" : NumberInt(677)
                }, 
                "auditInfo" : {
                    "version" : "1.0.0.0", 
                    "created_on" : ISODate("2016-08-08T07:27:01.000+0000")
                }, 
                "additionalAttributes" : {

                }
            }
        }
    }
]
}

以下是本机mongo查询 -

db.txnlog.aggregate([
{

  $group : {_id : "$pos_txn_type", count : {$sum : 1}, route_sum : {$push : "$inflight_transactions.0.message_exchange.request.domain_request.transactionAmount.amount"}}
}
])

和剪切输出 -

{ "_id" : "RECONCILE_REQUEST", "count" : 9, "route_sum" : [ [ ], [ ], [ ], [ ], [ ], [ ], [ ], [ ], [ ] ] }
{ "_id" : "CREDIT_CAPTURE", "count" : 2, "route_sum" : [ [ ], [ ] ] }
{ "_id" : "CREDIT_AUTHORIZATION", "count" : 2, "route_sum" : [ [ ], [ ] ] }

问题我没有得到金额总和?请提供一些意见。

1 个答案:

答案 0 :(得分:1)

您不能使用点表示法访问组舞台上的数组元素,您必须向$project inflight_transactions数组添加额外的$slice阶段。这是查询:

db.txnlog.aggregate([
    {$project: {
        pos_txn_type: 1,
        inflight_transaction: {$slice: ["$inflight_transactions", 1]}
    }},
    {$group: {_id: "$pos_txn_type",
               count: {$sum: 1},
               route_sum: {$push: "$inflight_transaction.message_exchange.request.domain_request.transactionAmount.amount"}
             }
    }
]);

// output
{ "_id" : "CREDIT_AUTHORIZATION", "count" : 1, "route_sum" : [ [ 100 ] ] }